Last active
May 22, 2023 17:02
-
-
Save elzup/886e47e2f13d00e4407b8efe5e71eb81 to your computer and use it in GitHub Desktop.
Goal: 100 Arrow functions, Anonymous function, lambda, closure example.
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
... | |
checks | |
// no args | |
// block | |
// implicit parameter | |
// assign |
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
const list = [1, 2, 3] | |
list.map(v => v * v) | |
list.map(function (v) { return v }) | |
// [ 1, 4, 9 ] | |
// no arg | |
list.map(() => 0) | |
// block | |
list.map(v => { | |
return 0 | |
}) | |
// implicit parameter | |
list.map(() => arguments[0] * arguments[0]) | |
// assign | |
const add = (a, b) => a + b | |
add(1, 2) |
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
list(map(lambda v: v * v, [1, 2, 3])) | |
# [1, 4, 9] | |
# no args | |
map(lambda: 0, [1, 2, 3]) | |
# block: none | |
# implicit parameter: none | |
# assign | |
add = lambda a, b: a + b | |
add(1, 2) |
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
[1, 2, 3].map { |v| v * v } | |
# [1, 4, 9] | |
# no args | |
[1, 2, 3].map { 0 } | |
# block | |
[1, 2, 3].map do | |
0 | |
end | |
# implicit parameter: none | |
# assign | |
add = ->(a, b) { a + b } | |
add.call(1, 2) | |
add.(1, 2) | |
double = ->(v) { v * 2 } | |
[11, 22].map &double |
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
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.BiFunction; | |
import java.util.stream.Stream; | |
class Main { | |
public static void main(String[] args) { | |
List<Integer> list = Arrays.asList(1, 2, 3); | |
Stream<Integer> res0 = list.stream().map(v -> v * v); | |
// no args: none | |
// block | |
Stream<Integer> res1 = list.stream().map(v -> { | |
return 0; | |
}); | |
// implicit parameter: none | |
// assign | |
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b; | |
// add.apply(1, 2) | |
} | |
} |
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
val list = listOf(1, 2, 3) | |
list.map { v -> v * v } | |
// [1, 4, 9] | |
// no args | |
list.map { 0 } | |
// block: none | |
// implicit parameter | |
list.map { it * it } | |
// assign | |
val add = { x: Int, y: Int -> x + y } | |
add(1, 2) |
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
<?php | |
array_map(fn($v) => $v * $v, [1, 2, 3]); | |
// [1,4,9] | |
// no args | |
array_map(fn() => 0, [1, 2, 3]); | |
// block | |
array_map(function($v) { | |
return 0; | |
}, [1, 2, 3]); | |
// implicit parameter: none | |
// assign | |
$add = fn($a, $b) => $a + $b; | |
$add(1, 2); |
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
[1, 2, 3].map { (v) -> Int in v * v } | |
// [1, 4, 9] | |
// no arg: none | |
// block | |
[1, 2, 3].map { (v) -> Int in | |
return 0 | |
} | |
// implicit parameter | |
[1, 2, 3].map { $0 * $0 } | |
// assign | |
let add = { (a: Int, b: Int) -> Int in a + b } | |
let add2: (Int, Int) -> Int = { (a, b) in a + b } | |
let add3: (Int, Int) -> Int = { $0 + $1 } | |
let add4: (Int, Int) -> Int | |
add4 = { $0 + $1 } | |
add(1, 2) |
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
[1, 2, 3].map(v => v * v) | |
[1, 2, 3].map((v: number): number => v * v) | |
// [ 1, 4, 9 ] | |
// no arg | |
[1, 2, 3].map(() => 0) | |
// block | |
[1, 2, 3].map(v => { | |
return 0 | |
}) | |
// implicit parameter: none | |
// assign | |
const add = (a: number, b: number): number => a + b | |
const add2: (a: number, b: number) => number = (a, b) => a + b | |
add(1, 2) |
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
[1, 2, 3].map (v) -> v * v | |
[1, 2, 3].map (v) => v * v | |
# [ 1, 4, 9 ] | |
# no arg | |
[1, 2, 3].map -> 0 | |
# block | |
[1, 2, 3].map -> | |
0 | |
# implicit parameter: none | |
# assign | |
add = (a, b) -> a + b | |
add 1, 2 |
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
[1, 2, 3].map (v) -> v * v | |
# [ 1, 4, 9 ] | |
# no arg | |
[1, 2, 3].map -> 0 | |
# block | |
[1, 2, 3].map -> | |
0 | |
# implicit parameter | |
[1, 2, 3].map (-> it * it) | |
[1, 2, 3].map (-> @@0 * @@0) | |
# assign | |
add = (a, b) -> a + b | |
add 1, 2 |
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
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
vector<int> list{ 1, 2, 3 }; | |
auto res = vector<int>(3); | |
transform(list.begin(), list.end(), res.begin(), [](int x){return x * x;}); | |
// no args: none | |
// block | |
transform(list.begin(), list.end(), res.begin(), [](int x){ | |
return 0; | |
}); | |
// implicit parameter: none | |
// assign | |
auto add = [](int a, int b) { return a + b; }; | |
auto add2 = [](int a, int b) -> int { return a + b; }; | |
add(1, 2); | |
} |
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
package main | |
func call(v int, callback func(int) int) int { return callback(v) } | |
func main() { | |
call(3, func(a int) int { return a * a }) | |
// omit args: none | |
// block | |
call(3, func(a int) int { | |
return 0 | |
}) | |
// implicit parameter: none | |
// assign | |
add := func(x int, y int) int { return x + y } | |
add(1, 2) | |
} |
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
fn main() { | |
let nums = vec![1, 2, 3]; | |
let res1: Vec<i32> = nums.iter().map(|v| v * v).collect(); | |
// [1, 4, 9] | |
// no args | |
let res2: Vec<i32> = nums.iter().map(|_| 0).collect(); | |
// [0, 0, 0] | |
// block | |
let res3: Vec<i32> = nums.iter().map(|v| { | |
v * 2 | |
}).collect(); | |
// [2, 4, 6] | |
// implicit parameter: none | |
// https://github.com/rust-lang/rfcs/issues/2554 | |
// assign | |
let triple = |v| v * 3; | |
let res5: Vec<i32> = nums.iter().map(triple).collect(); | |
// [3, 6, 9] | |
} |
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
my @list = (1, 2, 3); | |
$,=','; | |
$\="\n"; | |
map { $_ * $_ } @list; | |
# 1,4,9 | |
# no args | |
map { 0 } @list; | |
# block | |
map { | |
$_; | |
} @list; | |
# implicit parameter | |
map { $_ * $_ } @list; | |
# assign | |
my $add = sub { $_[0] + $_[1] }; | |
$add->(1, 2); |
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
list = [1, 2, 3] | |
# IO.inspect | |
list |> Enum.map(fn v -> v * v end) | |
# [1, 4, 9] | |
# no args: none | |
list |> Enum.map(fn _ -> 0 end) | |
# block | |
list |> Enum.map(fn _ -> | |
0 | |
end) | |
# implicit parameter | |
list |> Enum.map(&(&1 * &1)) | |
# assign | |
add = fn (a, b) -> a + b end | |
add.(1, 2) |
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
list = [1, 2, 3] :: [Int] | |
main = do | |
map (\v -> v * v) list | |
-- [ 1, 4, 9 ] | |
-- no args: none | |
-- block: none | |
-- implicit parameter | |
map (^2) list; | |
-- assign | |
let add a b = a + b | |
add 1 2 |
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
object Main { | |
def main(args: Array[String]): Unit = { | |
val list = Seq(1, 2, 3) | |
list.map((v) => v * v) | |
// List(1, 4, 9) | |
// no args: none | |
println(list.map((_) => 0)) | |
// block: none | |
// implicit parameter | |
list.map(_ * 2) | |
// List(2, 4, 6) | |
// assign | |
var add = (a: Int, b: Int) => a + b | |
add(1, 2) | |
// 3 | |
} | |
} |
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
-- _ = require 'underscore' -- Lua doesn't have HOC, get from lib | |
local list = {1, 2, 3} | |
_.map(list, function(v) return v * v end) | |
-- [ 1, 4, 9 ] | |
-- no arg | |
_.map(list, function() return 0 end) | |
-- block | |
_.map(list, function() | |
return 0 | |
end | |
) | |
-- implicit parameter: none | |
-- assign | |
local add = function(a, b) return a + b end | |
print(add(1, 2)) |
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
final list = [1, 2, 3]; | |
void main() { | |
list.map((v) => v * v).toList(); | |
// [ 1, 4, 9 ] | |
// no arg: none | |
// block: none | |
// implicit parameter: none | |
// assign | |
final add = (a, b) => a + b; | |
add(1, 2); | |
} |
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
my @list = 1..3; | |
say @list.map(-> $a { $a * $a }); | |
# (1 4 9) | |
# no arg | |
say @list.map({ 0 }); | |
# block | |
say @list.map(-> $a { | |
1; | |
0 | |
}); | |
# implicit parameter | |
say @list.map({$_ * $_}); | |
# (1 4 9) | |
# assign | |
my &add = -> $a, $b { $a + $b }; | |
my &add2 = { $^a + $^b }; | |
say &add(1, 2); | |
# 3 |
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
list = [1 to 3] | |
list.map (v) -> v * v | |
# [1,4,9] | |
# no arg | |
list.map -> 0 | |
# block | |
list.map -> | |
1 | |
0 | |
# implicit parameter | |
list.map -> &0 * &0 | |
# [1,4,9] | |
# assign | |
add = (a, b) -> a + b | |
add(1, 2) |
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
list = [1 to 3] | |
list = 1:3 | |
map(x-> x * x, list) | |
# Vector{Int64}: | |
# 1 | |
# 4 | |
# 9 | |
# no arg: none | |
map(_ -> 0, list) | |
# block | |
map(v -> begin | |
0 | |
v * v | |
end, list) | |
map(list) do v | |
1 | |
2 | |
end | |
# implicit parameter: none | |
# assign | |
add = (a, b) -> a + b | |
add(1, 2) |
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
import std/sugar | |
import sequtils | |
let list = @[1, 2, 3] | |
echo map(list, (x) => x * x) | |
echo list.map (x) => x * x | |
# @[1, 4, 9] | |
# no arg: none | |
let na = proc: int = 0 | |
# block | |
echo list.map (x) => | |
# | |
x * x | |
# implicit parameter: none | |
# assign | |
let add = (a: int, b: int) => a + b | |
echo add(1, 2) | |
# | |
# without suger | |
import sequtils | |
let list = @[1, 2, 3] | |
map(list, proc(x: int): int = x * x) | |
# @[1, 4, 9] | |
list.map do (x:int) -> int : x * x | |
# no arg: none | |
map(list, proc(x: int): int = 0) | |
# block | |
map(list, proc(x: int): int = | |
# | |
result = x * x | |
) | |
# implicit parameter: none | |
# assign | |
let add = proc(a: int, b: int): int = a + b | |
add(1, 2) | |
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
import std/sugar | |
import sequtils | |
list = [1, 2, 3] | |
map (x: "foo" + x) [ "bar" "bla" "abc" ] | |
echo map(list, (x) => x * x) | |
echo list.map (x) => x * x | |
# @[1, 4, 9] | |
# no arg: none | |
let na = proc: int = 0 | |
# block | |
echo list.map (x) => | |
# | |
x * x | |
# implicit parameter: none | |
# assign | |
let add = (a: int, b: int) => a + b | |
echo add(1, 2) | |
# | |
# without suger | |
import sequtils | |
let list = @[1, 2, 3] | |
map(list, proc(x: int): int = x * x) | |
# @[1, 4, 9] | |
list.map do (x:int) -> int : x * x | |
# no arg: none | |
map(list, proc(x: int): int = 0) | |
# block | |
map(list, proc(x: int): int = | |
# | |
result = x * x | |
) | |
# implicit parameter: none | |
# assign | |
add = { a, b }: a + b | |
add { a="hello"; b="world"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
* I'm biginner, REFACTOR ME