Matheus Brasil
Web alchemist who loves evangelizing cool technologies such as Javascript, functional programming and NoSQL.
É
MUITO F$%&
--| Exemplos
quadrado :: Integer -> Integer
quadrado x = x * x
somaTres :: Integer -> Integer -> Integer -> Integer
somaTres x y z = x + y + z
fatorial :: Integer -> Integer
fatorial n = product [1..n]
--| Exemplos
map :: (a -> b) -> [a] -> [b]
filter :: (a -> Bool) -> [a] -> [a]
var add = function(a, b){
return a + b
}
checaIntervalo :: (Integral a) => a -> String
checaIntervalo 1 = "Um"
checaIntervalo 2 = "Dois"
checaIntervalo 3 = "Três"
checaIntervalo 4 = "Quatro"
checaIntervalo 5 = "Cinco"
checaIntervalo _ = "Não tá no intervalo"
--| Se lembram do fatorial?
fatorial :: (Integral a) => a -> a
fatorial 0 = 1
fatorial n = n * fatorial (n - 1)
quadrado = [ x * x | x <- nums, x > 2 ]
-- | Se lembram do map?
[x+3 | x <- [1,5,3,1,6]]
-- | Com um predicado...
[ 2*x | x <- [0..], x ^ 2 > 3
function forLoop(arr){
for(var i=0, length = arr.length; i<length; i++){
console.log(arr[i] + " °C")
}
}
function forEachLoop(arr){
arr.forEach(function(temp){
console.log(temp + " °C")
)}
}
function converToFar(arr){
var convertedArr = [];
for(var i = 0, length = arr.length; i<length; i++){
var convertedTemp = arr[i] * 9/5 + 32;
convertedArr.push(convertedTemp)
}
return convertedArr;
}
function mapToFar(arr){
return arr.map(function(temp){
return temp * 9/5 +32;
}
}
function freezingTemps(arr){
var subZero = []
for(var i = 0, length = arr.length; i<length; i++){
if(arr[i]<0){
subZero.push(arr[i])
}
}
}
function filterFreezing(arr){
return arr.filter(function(temp){
return temp < 0
})
}
function sumOfTemps(arr){
var sum = 0;
for(var i = 0, length = arr.length; i<length; i++){
sum += arr[i];
}
return sum;
}
function reduceSum(arr){
return arr.reduce(function(prev, next){
return prev + next;
},0)
}
Languages built on top of JavaScript, to enhance its good parts and rid its bad parts.
[1, 2] +++ [3, 4]
[1, 2] +++ 3
#=> V.S.
[1, 2].concat([3, 4]);
[1, 2].concat(3);
<[one two three]>
#=> V.S.
['one', 'two', 'three']
[1 2 3] |> map (* 2) |> sum
#=> V.S.
sum(
map((function(it){
return it * 2;
}))(
[1, 2, 3]));
[x ** y for x in [10, 20] for y in [2, 3]]
#=>
[100, 1000, 400, 8000]
each (.push \NUG), [['a'] ['b'] ['c']]
#=> [['a', 'NUG'], ['b', 'NUG'], ['c', 'NUG']]
map (* 2), [1 to 5]
#=> [2, 4, 6, 8, 10]
filter (< 3), [1 to 5]
#=> [1, 2]
fold (+), 0, [1 to 5]
#=> 15
npm install -g livescript
By Matheus Brasil
Veremos como conceitos antigos de programação funcional podem fazer a diferença nos dias atuais para desenvolvedores JS por meio do LiveScript: um pré-processador que, através de melhorias de sintaxe e outros recursos, acaba tornando-se uma ótima alternativa para se iniciar no mundo funcional. Depois deste NUG-CE - com first-class functions, high-order functions, currying, composição, pipes e outras coisas mais -, você nunca mais escreverá o mesmo código.
Web alchemist who loves evangelizing cool technologies such as Javascript, functional programming and NoSQL.