Last active
August 29, 2015 14:22
-
-
Save H2CO3/9ed39030a48e9e7cd746 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
###### 'C-style' syntax (braces and semicolons) ###### | |
-> return statements needed (except in one-expression lambdas) | |
// function := 'fn' ['<' Types... '>'] ident '(' Args... ')' ['->' RetType] block-statement | |
// local-function := function | |
// lambda := '(' args... ')' '->' statement | |
// args := [ arg { , arg } ] | |
// arg := ident [['::'] type] | |
// if 'statement' is an expression, then automatically 'return' it! | |
// 'statement' can be a block as well! (-> multiple statements...) | |
// verbose | |
fn <T, P> | |
filter(arr :: [T], pred :: P) -> [T] { | |
var result = [] :: type(arr); | |
arr.each((v, i) -> if pred(v) { result.push(v) }); | |
return result | |
} | |
// '::' is not compulsory | |
filter(arr [T], pred T -> bool) -> [T] { ... } | |
// concise | |
fn filter(arr, pred) { | |
var result = [] :: type(arr); | |
arr.each((v, i) -> if pred(v) { result.push(v) }); | |
return result | |
} |
This file contains hidden or 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
###### 'Lightweight' syntax (whitespace-sensitive) ###### | |
-> expression language; value of last statement is automatically returned | |
// lambda := 'fn' ['(' args ')'] ['->' RetType] '=' expression | |
// args := [ arg { , arg } ] | |
// arg := ident ['::' type] | |
// expression can be a 'one-liner' or a block | |
// verbose | |
fn<T> | |
filter (arr :: [T], pred :: T -> bool) -> [T] = | |
var result = [] :: [T] | |
arr.each( | |
fn (v :: T) -> () = | |
if pred(v) then | |
result.push(v) | |
() | |
) | |
result | |
// concise | |
fn filter (arr, pred) = | |
var result = [] :: type(arr) | |
arr.each( | |
fn (v) = | |
if pred(v) then | |
result.push(v) | |
) | |
result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment