Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Goal: 100 Arrow functions, Anonymous function, lambda, closure example.
...
checks
// no args
// block
// implicit parameter
// assign
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: none
// assign
const add = (a, b) => a + b
add(1, 2)
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)
[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
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)
}
}
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)
<?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);
[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)
[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)
[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
[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
#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);
}
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)
}
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]
}
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);
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)
list = [1, 2, 3, 4, 5] :: [Int]
main = do
map (\v -> v * v) list
-- [ 1, 4, 9 ]
-- no args: none
-- block: none
-- print $ map (\() -> 0 ) list
-- implicit parameter
map (^2) list;
-- assign
let add a b = a + b
add 1 2
@elzup

This comment has been minimized.

Copy link
Owner Author

@elzup elzup commented Feb 27, 2020

  • TypeScript
  • CoffeeScript
  • Elm *
  • Rust *
  • D *
  • C++ *
  • Go *
  • ShellScript
  • C# *
  • Coco *
  • Kotlin *
  • Swift *
  • Elixir *
  • Perl *
  • Haskell *
  • Dart *

* I'm biginner, REFACTOR ME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.