Skip to content

Instantly share code, notes, and snippets.

View NicolasT's full-sized avatar

Nicolas Trangez NicolasT

View GitHub Profile
val a = 1 :: 2 :: 3 :: 4 :: 3 :: 2 :: 5 :: 4 :: Nil
def r[A](h: List[A], t: List[A]): List[A] = {
if(t isEmpty)
return h
if(t.tail exists(i => i == t.head))
h ::: r(Nil, t.tail)
else
h ::: (t.head :: Nil) ::: r(Nil, t.tail)
object Profiler {
def apply[A](report: Long => Unit)(fun: => A): A = {
val start = System.currentTimeMillis()
val result = fun
val time = System.currentTimeMillis() - start
report(time)
result
}
def PrintProfiler[A](fun: => A) =
def fact(x: Int): Int = fact(x, 1)
def fact(x: Int, acc: Int): Int = x match {
case 0 => acc;
case n => fact(n - 1, x * acc)
}
val facts = (0 to 10).toList.map(fact)
println("Facts: " + facts)
nicolas@lambda ~/Projects/Playground/fact $ cat fact.scala
def fact(x: BigInt): BigInt = fact(x, 1)
def fact(x: BigInt, acc: BigInt): BigInt = x match {
case _ if x == 0 => acc;
case n => fact(n - 1, x * acc)
}
//val facts = (0 to 10).toList.map(fact)
//println("Facts: " + facts)
nicolas@lambda ~/Projects/Playground/fact $ cat fact.scala
def fact(x: BigInt): BigInt = fact(x, 1)
def fact(x: BigInt, acc: BigInt): BigInt = x match {
case _ if x == 0 => acc;
case n => fact(n - 1, x * acc)
}
//val facts = (0 to 10).toList.map(fact)
//println("Facts: " + facts)
bind = function() # a -> m a
return_ = function() # m a -> (f a -> m b) -> m b
class Monad(TypeClass):
__functions__ = (
bind,
return_,
)
class Maybe(object): pass
import os
import os.path
import sys
import signal
from twisted.application import service
from twisted.internet import protocol, reactor
from twisted.python import log
from twisted.scripts._twistd_unix import ServerOptions
# State is represented as (value, state)
returnST = lambda a: lambda s: (a, s)
# The 'ss' thing is some trick to get around the inability to assign in a
# lambda expression, and get around calculating x(s) twice
bindST = lambda x: lambda f: lambda s: \
(lambda ss: runST(f(ss[0]))(ss[1]))(x(s))
runST = lambda f: lambda s: f(s)
# No warranties on this code whatsoever...
import ctypes
py_valloc = ctypes.pythonapi.valloc
py_free = ctypes.pythonapi.free
py_memmove = ctypes.pythonapi.memmove
py_mprotect = ctypes.pythonapi.mprotect
NULL = 0x0
# Some code to retrieve CPUID information in pure Python
#
# Copyright (C) 2009 Nicolas Trangez <eikke eikke com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1
# of the License.
#
# This library is distributed in the hope that it will be useful,