Last active
July 8, 2017 23:21
-
-
Save davidrhyswhite/d27d0621db842305094a4db4e09fd1ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Conditionals | |
if (myVar == 'Hello') { | |
true | |
} | |
// if (myVar === 'Hello') { | |
// return true; | |
// } | |
// Only strict equality allowed for simplicity. Conditional expression returns by default the last expression. | |
// Single line conditionals | |
if (myVar == 'Hello') true | |
// Same as above, | |
// Negated conditional | |
unless (myVar == 'Hello') { | |
'World' | |
} | |
// if (myVar !== 'Hello') { | |
// return 'World'; | |
// } | |
// Conditional expression ordering can be reversed. | |
{ | |
'World' | |
} unless (myVar == 'Hello') | |
// Conditional expressions return a value and can be assigned | |
say = unless (myVar == 'Hello') 'Hi!' | |
// const say = (myVar !== 'Hello') ? Some('Hi!') : None; | |
// Single line reversed conditionals | |
say = 'World' unless (myVar == 'Hello') | |
// const say = (myVar !== 'Hello') ? Some('World') : None; | |
// Conditionals with ambiguous returns auto-magically return an Option-al Monad | |
say = 'Hello' unless (language == 'Welsh') | |
// const say = (language !== 'Welsh') ? Some('Hello') : None; | |
// Inline else will return more Option-al values | |
say = 'Helo' if (language == 'Welsh') else 'Hello' | |
// const say = (language === 'Welsh') ? Some('Helo') : Some('Hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myNumber = 10 | |
enum Color { | |
green, red, blue, yellow | |
} | |
getGreen (color: Color): Optional(Color) { | |
match color { | |
color.green { | |
console.log('The color is green') | |
} | |
color.red, color.blue { | |
console.log('The color is not green but red or blue') | |
} | |
color.yellow unless (myNumber == 10) { | |
console.log('The color is yellow and myNumber is 10') | |
} | |
_ { | |
console.log('No match, _ is now an Optional.None') | |
} | |
} | |
} | |
getGreen(Color.green) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pattern matches allow matching types or values and the expressions return the match, | |
// single line expressions run the expression and return the match wrapped in a an Optional. | |
// We denote a single line expression with the -> operator as with functions. | |
x = 2 | |
myMatch = match x { | |
1 || 2 -> console.log("one or two? x was equal to " ++ x) | |
3 -> console.log("x was three") | |
_ -> console.log("anything") | |
} | |
// Should evaluate to: | |
const x = 2; | |
let _myMatch; | |
switch (x) { | |
case (1 || 2): { | |
console.log("one or two? x was equal to " + x); | |
_myMatch = Optional(x); | |
} | |
case 3: { | |
console.log("x was three") | |
_myMatch = Optional(x); | |
} | |
default: { | |
_myMatch = Optional.None; | |
} | |
} | |
const myMatch = Object.freeze(_myMatch); | |
// We can also provide an expression with the parethensies, to allow longer expressions, these also return an Optional type. | |
myMatch = match x { | |
1 || 2 { | |
console.log("one or two? x was equal to " ++ x) | |
} | |
3 { | |
console.log("x was three") | |
} | |
_ { | |
console.log("anything") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// As we see we can create simple 1 line function closures, we also could pipe | |
negate (func)(x) -> !func(x) | |
// Instead we could use a UNIX pipe operator | |
x | !func | |
// Or | |
!(x | func) | |
// This is useful when composing many small functions, given the following: | |
myNumber = Math.round(Math.sqrt(Math.PI)) | |
// could be written: | |
myNumber = Math.PI | Math.sqrt | Math.round |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functions | |
// Longhand declaration | |
negate (func, x) { | |
!func(x) | |
} | |
negate (func) { | |
(x) { | |
!func(x) | |
} | |
} | |
// Shorthand declaration | |
negate (func)(x) { | |
!func(x) | |
} | |
// Shorter-hand declaration where single line methods are possible | |
negate (func)(x) -> !func(x) | |
apply(func, array) { | |
map(array, (element) { | |
func(element) | |
}) | |
} | |
// | |
apply(func, array) -> map(array, (element) -> func(element)) | |
// Execution | |
is_not_nan = negate(is_nan)('not a number') | |
apply(Math.round, [0.01, 2, 9.89, Math.PI]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stringConstant: String = 'a type safe constant' | |
numberConstant: Number = 10 | |
functionConstant: Function = (){} | |
functionArgAndReturnTypes (arg1: String, arg2: Number): Array[_] { | |
[arg1, arg2] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constants declaration | |
myVar = 'a constant variable' | |
// const myVar = 'a constant variable'; but also typed to a String by default. | |
myOtherVar = 34 | |
// const myOtherVar = 34; but typed to a Number this time. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| | |
| leftExp == rightExp | leftExp === rightExp | |
| leftExp != rightExp | leftExp !== rightExp | |
| leftExp >= rightExp | Same | |
| leftExp <= rightExp | Same | |
| leftExp || rightExp | | |
| leftExp -> rightExp | const leftExp = function () { return rightExp; } | |
| leftExp | rightExp | rightExp(leftExp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment