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
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) |
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
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) = |
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 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) |
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
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) |
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
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) |
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
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 |
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 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 |
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
# 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) |
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
# 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 |
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 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, |