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 f = (a, b, c) => [a, b, c] | |
| f() // [ undefined, undefined, undefined ] | |
| f(1) // [ 1, undefined, undefined ] | |
| f(1, 2) // [ 1, 2, undefined ] | |
| f(1, 2, 3) // [ 1, 2, 3 ] | |
| const f = (...args) => args | |
| f(1, 2, 3) // [ 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
| const sum = (a, b) => a + b | |
| sum(3, 4) | |
| const neg = n => -n | |
| neg(10) | |
| globalThis.performance.now() | |
| const getGlobalContext = () => typeof window === 'object' ? ('Deno' in window ? globalThis : window) : global |
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
| // Initial try! Does not work, because it updates the counter for the 1st recursive call only. | |
| const countRecursiveCalls = fn => { | |
| let count = 0 | |
| const wrapperFn = (...args) => ( | |
| count++, | |
| fn(...args) | |
| ) | |
| wrapperFn.getCount = () => count |
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 fibonacci = n => { | |
| if (n <= 1) return n | |
| return fibonacci(n - 1) + fibonacci(n - 2) | |
| } | |
| // One liner. | |
| const fibonacci = n => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2) | |
| let count |
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
| Array.prototype.myForEach = function(fn) { | |
| for (let i = 0; i < this.length; i++) | |
| fn(this[i], i) | |
| } | |
| Array.prototype.myMap = function(fn) { | |
| const results = [] | |
| this.myForEach((item, i) => { | |
| results.push(fn(item, i)) |
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 a = [1, 2, 3, 4, 5] | |
| undefined | |
| > a.reduce((acc, item, index) => acc + item) | |
| 15 | |
| > a.reduce((acc, item, index) => acc + item, 0) | |
| 15 | |
| > a.reduce((acc, item, index) => acc * item) | |
| 120 | |
| > a.reduce((acc, item, index) => acc * item, 1) | |
| 120 |
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 a = [1, 2, 3, 4, 5] | |
| undefined | |
| > | |
| undefined | |
| > a.forEach( (item, i) => { console.log([i, item]) } ) // High-Order Fn (HOF) | |
| [ 0, 1 ] | |
| [ 1, 2 ] | |
| [ 2, 3 ] | |
| [ 3, 4 ] | |
| [ 4, 5 ] |
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
| // Regras: | |
| // | |
| // 1) O processo de matching começa sempre da esquerda para a direita | |
| // 2) O algoritmo para no primeiro match | |
| // 3) Ao dar match, o algoritmo tenta reconhecer o maior número possível de caracteres (“greeedy” = guloso, ganancioso) | |
| > const showRegexp = (str, re) => { | |
| const match = str.match(re) | |
| return match ? |
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
| class Array | |
| def my_each(&my_proc) | |
| for e in self | |
| my_proc.call(e) if my_proc | |
| end | |
| end | |
| end | |
| sum = 0 | |
| [1, 2, 3].my_each { |x| sum += x } |
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 'benchmark' | |
| an_array = (1..37000).to_a | |
| a_hash = (1..37000).inject({}) { |acc, i| acc[i] = true; acc } | |
| array_time = Benchmark.realtime { (1..37000).each { |i| an_array.include?(i) } } | |
| hash_time = Benchmark.realtime { (1..37000).each { |i| a_hash.has_key?(i) } } | |
| puts array_time / hash_time |
NewerOlder