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
; Some imports we need | |
(import '(java.net ServerSocket Socket SocketException) | |
'(java.io InputStreamReader BufferedReader OutputStreamWriter) | |
) | |
; Helper to run a callable in a new thread | |
(defn threaded [fun] | |
; Create a new thread | |
(let [thread (new Thread fun)] | |
; And start it |
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
import time | |
import functools | |
def fib(n, fun=None): | |
'''A naive fib function, taking the recursion callable as an argument''' | |
if n == 0 or n == 1: return n | |
fun = fun or fib | |
return fun(n - 1) + fun(n - 2) | |
def fib2(n): |
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
// This type is defined in some other library we can't modify | |
class SomeObject(val a: Int, val b: String) { | |
def c = "a = " + a + ", b = " + b | |
} | |
// This is our code | |
object DynamicDemo { | |
// A function which takes anything which has a member 'a' of type Int and a | |
// member 'd' of type String | |
def f(arg: {def a: Int; def d: String}) = "f says: " + arg.a + " " + arg.d |
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
(env-2.6)MacBook:dynamic_scala nicolas $ cat DynamoDemo.scala | |
// This is a class in some library | |
class ExternalClass(val a: Int, val b: String) | |
class InternalClass(val i: Int) { | |
val j = i * 2 | |
} | |
object DynamoDemo { | |
def f(a: ExternalClass) = "" + a.a + " " + a.b |
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
def interleave(*iters): | |
iters = [iter(i) for i in iters] | |
while True: | |
stop_loop = False | |
values = list() | |
for i in iters: | |
try: | |
values.append(i.next()) | |
except StopIteration: |
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
interface Function1<T1, R> { | |
public R apply(T1 v); | |
} | |
public class Functional { | |
public static void main(String[] args) { | |
Integer[] i = {1, 2, 3, 4}; | |
foreach(new Function1<Integer, Object>() { | |
public Object apply(Integer v) { | |
System.out.println("" + v); |
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
In [1]: # Our definition of fib | |
In [2]: fib = lambda f: lambda n: 1 if n in (0, 1) else f(n - 1) + f(n - 2) | |
In [3]: # The Z fixpoint combinator | |
In [4]: Z = lambda f: (lambda x: f(lambda *args: x(x)(*args)))(lambda x: f(lambda *args: x(x)(*args))) | |
In [5]: # Works as expected |
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
args.extend( | |
map(str, | |
itertools.chain( | |
*list( | |
(key, value) if value is not None else (key, ) | |
for (key, value) in process_args.iteritems() | |
) | |
) | |
) | |
) |
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
__author__ = 'Nicolas Trangez' # :-P | |
class Package(object): | |
'''Basic package representation''' | |
__slots__ = 'name', 'dependencies', | |
def __init__(self, name, dependencies=None): | |
'''Initialize a new package | |
:param name: package name |
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
def gen_pi(): | |
denom = 1. | |
positive = True | |
value = 0. | |
while True: | |
value += (1. / denom) if positive else -(1. / denom) | |
denom += 2. | |
positive = not positive | |
yield 4. * value |
OlderNewer