$ python3
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
Shameful mistake, thanks for clarifying ! I meant that they had their own type now, indeed from integer.
Full explanation
Initially, Python used to have no bool type
(people used 0 for false and non-zero value like 1 for true).
Then they added True, False, and a bool type, but, for backward
compatibility, they couldn't make True and False constants- they
just were built-in variables.
A PowerShell script to get 5 most CPU intensive processes.
PowerShell passes .NET CLI object(s) into the next pipeline (<-- this is most unique feature of all shells) so there is need to parse text as you do in other shells (using sed or awk).
Woah. How do rational numbers interact with irrational numbers in this case? Are they approximated prior to any sort of arithmetic or are they considered incompatible types?
π and 1/2 were my two motivating reasons for making this feature.
For irrationals, and other fractions, the system will switch to a high-precision floating point mode instead of a rational -- I honestly don't remember what the trigger criteria actually is, I think size of the rational.
What this allows however is that π is a single constant you can define to 100 decimal places. This is high enough precision that several constant operations, basic math, can work on this value without losing precision when converting to the actual system type. It doesn't matter if you convert to a 32/64/128-bit float, it'll have the full precision for that type. No need for constants per type, or stuff like M_PI2.
Note this precision only applies to constants. Runtime variables are limited by the standard system types. Sometime later I'll add these numbers are runtime, but they're fairly special purpose at that point. The literal folding now is enough to cover the current intended use-cases.
Kotlin has this concept of infix functions, which means they are an extension function that has only 1 parameter and allows us to use them without the dot (.) operator and no parentheses (but you can also do it the normal way, if you want to).
Declare the infix function
infixfunInt.times(base:Int)=this*base
Example
funmain(args:Array<String>){println(10times5)// prints 50println(10.times(5))// also prints 50}
Javascript... Yes, Javascript is a barrel of pure quirk. Just pick variable scope. As if it wasn't bad enough that hoisting exists, the bizarre way scopes work allows you to write some truly awe-inspiring hackery, like this Fibonacci function:
It isn't that difficult to generate the Fibonacci Spiral through Oracle SQL :)
with FIBONACCI (i, FIBO, PREV) as
(
select
1 i,
0 FIBO,
cast( null as number ) PREV
from
DUAL
union all
select
f1.i + 1 i,
f1.FIBO + nvl(f1.PREV,1) FIBO,
f1.FIBO PREV
from
FIBONACCI f1
where
f1.i < 20
)
select
FIBO
from
FIBONACCI
order by
i;
Thanks to the batshit insane way that this works, it's a full-blown method.
This capability isn't unusual. In Python, you can also add methods to instances, but you need to ensure you handle self:
classFoo:def__init__(self):# Some codef=Foo()dir(f)## Returns ['__doc__', '__init__', '__module__']Foo.method=lambdaself,x:xdir(f)## Now includes our new methodf.method(22)## Returns 22.
Languages such as C++ and Java, on the other hand, have immutable types (and in the former, types aren't themselves even a type).
There are lots of things I like about F#. Probably one that is unique though is its computation expressions. Here is a list example.
// a list normally is defined like this: [ 1; 2; 3 ]letvalidatecourse=[// but the compiler knows this is a CE// because we put logic in itifisBlankishcourse.Namethenyield"Course Name blank"matchcourse.Examwith|None->()// do nothing|Someexam->yield!Exam.validateexam// the ! is for other lists, flattens]...// errors : string listleterrors=validatecourse
This provides a list of errors. And it is composable with other validations. It could be called in the same way that this function calls Exam.validate.
And another one. C++ can do some pretty impressive things at build time. In the case of Fibonacci, we can declare it as a template function, and let it get calculated during compilation:
This (I think) is still calculating the result at compile time - but if we used a variable instead of 20, it'd calculate it at compile time. As far as whether the code would recurse twice at each level, though, that's up to how clever the compiler is feeling - we've told it (with constexpr that the results are always the same).
Not a function but a fun little bit of code. It's not all that interesting but it shows how easy it is to write code without defining variables in Elixir, which cleans up your code a lot!
eval(atob('QXJyYXkoMTYpLmpvaW4oImEiLTEpICsgIiBCYXRtYW4hIjs')) safe and fun to run and related to Batman! Jokes aside, to summarize, this guy pushes JavaScript to the limits: aem1k.com/
Unique and interesting, but awful
Python 2.X:
Python is full of very tricky things ! I had a lot of fun reading this README which explains them.
never know it. it looks like js nightmare. :)
Fortunately it's gone in Python 3:
:D
It's no longer an integer :)
It still is. bool is a subclass of int:
Shameful mistake, thanks for clarifying ! I meant that they had their own type now, indeed from integer.
Full explanation
That readme is excellent, thanks for posting it!
Ah!!! Looks like Python is a distant relative of Javascript!!
In Ruby we can monkeypatch to easily add functionality to any class.
For example:
I'm extending the string class so that
"hello".yell!
outputs"HELLO!!!"
And now all strings in the program have access to the
yell!
method. ❤️I'll add that this is sort of bonkers and an easy way to add some really hard-to-debug code to an app. Use with great fear and caution.
That's amazing that Ruby and other languages can extend built-in classes as well.
Anyways, to show off C#, here you go.
C# can extend any classes using extension method syntax.
Outputs
Note that you should mark the parameter with
this
Here's how this would look in Kotlin:
Another interesting use is to create an Extension Property so you can print any type to the console, like this:
Which can be used like:
Since this is also available on any other
class
, any object you create will also have this property and it will call theirtoString()
.In F# it is also easy to add functionality to existing classes.
You can also add functions to existing static classes (modules) too.
Lots of languages support that, including most obviously Javascript.
Interesting. Is that possible via class definitions (class syntax I mean) or only via the prototype syntax?
A PowerShell script to get 5 most CPU intensive processes.
PowerShell passes .NET CLI object(s) into the next pipeline (<-- this is most unique feature of all shells) so there is need to parse text as you do in other shells (using
sed
orawk
).This is pretty neat
Thanks Vinay.
Leaf stores literals (constants) as rational numbers and is type safe.
good
is assigned the value1
-- there is no precision loss on the division.err
produces a compiler error since1/3*2
is not an integer.Woah. How do rational numbers interact with irrational numbers in this case? Are they approximated prior to any sort of arithmetic or are they considered incompatible types?
π
and1/2
were my two motivating reasons for making this feature.For irrationals, and other fractions, the system will switch to a high-precision floating point mode instead of a rational -- I honestly don't remember what the trigger criteria actually is, I think size of the rational.
What this allows however is that
π
is a single constant you can define to 100 decimal places. This is high enough precision that several constant operations, basic math, can work on this value without losing precision when converting to the actual system type. It doesn't matter if you convert to a 32/64/128-bit float, it'll have the full precision for that type. No need for constants per type, or stuff likeM_PI2
.Note this precision only applies to constants. Runtime variables are limited by the standard system types. Sometime later I'll add these numbers are runtime, but they're fairly special purpose at that point. The literal folding now is enough to cover the current intended use-cases.
Kotlin has this concept of
infix
functions, which means they are an extension function that has only 1 parameter and allows us to use them without the dot (.) operator and no parentheses (but you can also do it the normal way, if you want to).Declare the
infix
functionExample
JavaScript lets you swap variables without a temporary variable.
So does C:
OK, so this is specific to numeric values, actually, but it comes in handy in cryptographic code for constant time conditional swaps and things.
Thanks Dave.
There is no end to learning.
I feel humbled :)
Same in Python
Python has been on my mind lately. That's yet another good reason ;p
Javascript... Yes, Javascript is a barrel of pure quirk. Just pick variable scope. As if it wasn't bad enough that hoisting exists, the bizarre way scopes work allows you to write some truly awe-inspiring hackery, like this Fibonacci function:
It isn't that difficult to generate the Fibonacci Spiral through Oracle SQL :)
Right - something like this is fine:
Thanks to the batshit insane way that
this
works, it's a full-blown method.This capability isn't unusual. In Python, you can also add methods to instances, but you need to ensure you handle
self
:Languages such as C++ and Java, on the other hand, have immutable types (and in the former, types aren't themselves even a type).
JavaScript
An interesting one I can think of in Python.
Reversing a string
How's written in other common languages too can be seen side by side
There are lots of things I like about F#. Probably one that is unique though is its computation expressions. Here is a list example.
This provides a list of errors. And it is composable with other validations. It could be called in the same way that this function calls
Exam.validate
.Pattern matching in elixir can be pretty cool
C# and null safe navigation
Saves sooo many null checks...
And another one. C++ can do some pretty impressive things at build time. In the case of Fibonacci, we can declare it as a template function, and let it get calculated during compilation:
In the above, there's no recursion at runtime - it's all in the compiler.
Or, in C++ 11 and beyond, we get
constexpr
, which will give us the same thing, but allow the function to be called normally at runtime as well:This (I think) is still calculating the result at compile time - but if we used a variable instead of
20
, it'd calculate it at compile time. As far as whether the code would recurse twice at each level, though, that's up to how clever the compiler is feeling - we've told it (withconstexpr
that the results are always the same).Not a function but a fun little bit of code. It's not all that interesting but it shows how easy it is to write code without defining variables in Elixir, which cleans up your code a lot!
eval(atob('QXJyYXkoMTYpLmpvaW4oImEiLTEpICsgIiBCYXRtYW4hIjs'))
safe and fun to run and related to Batman! Jokes aside, to summarize, this guy pushes JavaScript to the limits: aem1k.com/I made a codepen of a 4chan greentext about Javascript logic
Basically just having fun with on the fly type conversion
In bash:
for i in {1..12}; do for j in $(seq 1 $i); do echo -ne $i×$j=$((i*j))\t;done; echo;done
imgur.com/Z7jWFhI
Yeah - sorry, meant to demonstrate that you can attach a function via prototype to a class defined with class syntax.
F# typeproviders are just LOVE