Last active
December 14, 2015 20:39
-
-
Save jsoriano/5145457 to your computer and use it in GitHub Desktop.
Closures that modify a variable of these scopes in different languages
This file contains 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
module Main (main) where | |
import Debug.Trace | |
import Control.DeepSeq | |
import Control.Monad.State | |
type Counter = State Int () | |
log' :: Counter -> Counter | |
log' f = do f | |
modify (\c -> traceShow c c) | |
foo :: Counter | |
foo = modify (+1) | |
main :: IO () | |
main = do deepseq (runState (log' foo >> log' foo) 0) (return ()) |
This file contains 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
function foo() { | |
var a = 0; | |
var bar = function() { | |
a = a + 1; | |
return a; | |
} | |
return bar; | |
} | |
f = foo(); | |
console.log(f()); | |
console.log(f()); |
This file contains 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 | |
function foo() { | |
$a = 0; | |
return function() use (&$a) { | |
$a = $a + 1; | |
return $a; | |
}; | |
}; | |
$f = foo(); | |
echo $f(); | |
echo $f(); |
This file contains 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
# Tricky thing: variable to modify cannot be a simple variable, it can if it's only read and not modified | |
def foo(): | |
a = [0] | |
def bar(): | |
a[0] = a[0] + 1 | |
return a[0] | |
return bar | |
f = foo() | |
print f() | |
print f() |
This file contains 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
# Tricky thing: in ruby you cannot return named functions, but you can | |
# return lambdas (that need to be called with .call method) | |
def foo | |
a = 0 | |
return lambda { a = a + 1 } | |
end | |
f = foo | |
print f.call | |
print f.call |
Plz, if I don't see monads I get bored:
module Main (main) where
import Debug.Trace
import Control.DeepSeq
import Control.Monad.State
type Counter = State Int ()
log' :: Counter -> Counter
log' f = do f
modify (\c -> traceShow c c)
foo :: Counter
foo = modify (+1)
main :: IO ()
main = do deepseq (runState (log' foo >> log' foo) 0) (return ())
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added!