Skip to content

Instantly share code, notes, and snippets.

View NicolasT's full-sized avatar

Nicolas Trangez NicolasT

View GitHub Profile
; 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
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 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
(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
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:
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);
@NicolasT
NicolasT / gist:172397
Created August 21, 2009 20:11
A demonstration of usage of the Z fixed point combinator in Python
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
args.extend(
map(str,
itertools.chain(
*list(
(key, value) if value is not None else (key, )
for (key, value) in process_args.iteritems()
)
)
)
)
__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
@NicolasT
NicolasT / gist:177325
Created August 28, 2009 23:42
Calculate Pi using the Leibniz formula
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