Skip to content

Instantly share code, notes, and snippets.

View diegopacheco's full-sized avatar

Diego Pacheco diegopacheco

View GitHub Profile
@diegopacheco
diegopacheco / higher_order_funcs.hs
Created January 17, 2012 22:28
Higher Order Functions in Haskell
map (+1) [1, 2, 3, 4, 5] -- [2,3,4,5,6]
map succ [1, 2, 3, 4, 5] -- [2,3,4,5,6]
filter (>3) [1,5,3,2,1,6,4,3,2,1] -- [5,6,4]
@diegopacheco
diegopacheco / hof.hs
Created January 17, 2012 23:42
Higher Order Functions in Haskell
-- hof.hs
apply2 :: (a -> a) -> a -> a
apply2 f x = f (f x)
--
-- ghci hof.hs
--
apply2 (*2) 100 -- 400
@diegopacheco
diegopacheco / hof2.hs
Created January 17, 2012 23:51
Higher Order Functions in Haskell
map (\x -> x * 2) [1..4] -- [2,4,6,8]
@diegopacheco
diegopacheco / currying.scala
Created January 18, 2012 01:06
Currying in Scala
def add(n:Int)(a:Int) = n + a
val add2 = add(2)(_)
add(5)(5) // 10
add2(30) // 32
@diegopacheco
diegopacheco / currying2.scala
Created January 18, 2012 01:17
Currying in Scala
def benchmark(msg:String)(f:()=>Unit) = {
val init = System.currentTimeMillis
f
val end = System.currentTimeMillis()
println(msg + " in: " + (end - init) + " ms")
}
benchmark("for benchmark ")( () =>
for(i <- 1 to 1000){
println(i)
@diegopacheco
diegopacheco / currying.hs
Created January 18, 2012 01:35
Currying in Haskell
max 10 20 -- 20
(max 10) 20 -- 20
@diegopacheco
diegopacheco / currying2.hs
Created January 18, 2012 01:42
Currying in Haskell
-- currying.hs
mul2 :: (Num a) => a -> a -> a
mul2 x y = x * y
--
-- ghci currying.hs
--
mul2 2 3 -- 6
let double = mul2 2
@diegopacheco
diegopacheco / lambda.hs
Created January 18, 2012 02:04
Lambda in Haskell
map (\x -> x + 3) [1,6,3,2] -- [4,9,6,5]
map (\(a,b) -> a + b) [(1,2),(3,5),(6,3),(2,6),(2,5)] -- [3,8,9,8,7]
-- add.hs
addThree :: (Num a) => a -> a -> a -> a
addThree = \x -> \y -> \z -> x + y + z
--
-- ghci add.hs
@diegopacheco
diegopacheco / lambda.scala
Created January 18, 2012 02:10
Lambda in Scala
() => 2 +2 // () => Int = <function0>
(() => 2 + 2)() // 4
List(1,2,3,4,5).map( i => i + 1 ) // List(2, 3, 4, 5, 6)
List(1,2,3,4,5,6,7,8,9,10).filter( n => n >= 7 ) // List(7, 8, 9, 10)
List(1,2,3,4,5,6,7,8,9,10).filter( _ >= 7 ) // List(7, 8, 9, 10)
@diegopacheco
diegopacheco / bash_profile_tips.sh
Created January 22, 2012 17:33
How to Create a .bash_profile in Cygwin
cd /
mkdir /home/diegopacheco
echo "export HOME=/home/diegopacheco" >>.bashrc
echo "export HOME=/home/diegopacheco" >>.bash_profile
cp .bashrc .bash_profile /home/diegopacheco
echo "cd" >>.bashrc