Skip to content

Instantly share code, notes, and snippets.

View sadache's full-sized avatar

Sadek Drobi sadache

View GitHub Profile
type 'a SimpleType= F of (int ->'a-> 'a)
let get a = F(fun req -> id)
[<GeneralizableValue>]
let oops2<'T> : SimpleType<'T> = get ""
@sadache
sadache / scala-functions.scala
Created April 20, 2010 09:53
What is/is_not a function in Scala
object looksLikeAFunction{
def apply(s:String):Int=1
}
object nowAFunction extends Function[String,Int]{
def apply(s:String):Int=1
}
object TestMethods {
implicit def functionToAnother(f:Function[String,Int]):Function[Int,String] =
(i:Int)=> f(i.toString()).toString()
@sadache
sadache / lazy.scala
Created May 5, 2010 15:05
Scala lazy val? not really.
lazy val xxxx={ println("initializing xxxx");1}
def getOne(x:Int):Int= 1
println(getOne(xxxx));
@sadache
sadache / fibs.hs
Created May 5, 2010 15:16
my favourite piece of code ever
let fibs@(h:tfibs)= 1:2:zipWith (+) fibs tfibs
main = do resp <- simpleHTTP request
case resp of
Left x -> print ("Error connecting: " ++ show x)
Right r ->
case rspCode r of
(2,_,_) ->
print $ ( valFromObj "statuses_count"
=<< decode (rspBody r) :: Result Int)
where
request=Request {rqURI = uriSadache,
simpleGet uri error ok =
do resp <- simpleHTTP req
either error
(\r ->
case rspCode r of
(2,_,_) -> ok (rspBody r))
resp
where req=request urii
urii =fromJust $ parseURI uri
module Main where
class Monad64 a ma mb | ma mb -> a where
(>>==) :: ma -> ( a -> mb) -> mb
data Maybe64 a = Just64 a
|Nothing64
deriving Show
instance Monad64 a (Maybe64 a) (Maybe64 b) where
case class Error(msg:String)
val one:Either[Int,Error]= Left(1)
val two:Either[Int,Error]= Right(Error("oops"))
val result=for(a<-one.left; t <- two.left) yield a + t
// defined class Error
// one: Either[Int,Error] = Left(1)
// two: Either[Int,Error] = Right(Error(oops))
// result: Either[Int,Error] = Right(Error(oops))
// I really do not like using the syntax that removes points
scala> List("one","two") foldLeft ("") (_+_)
<console>:6: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
List("one","two") foldLeft ("") (_+_)
^
// Int ???
scala> List("one","two") foldLeft ("") ((_+_):(String,String) => String)
<console>:6: error: type mismatch;
case class Error[+E,+A](e:Either[E,A]) {
def flatMap[B,EE>:E](f:A => Error[EE,B]):Error[EE,B] ={
Error(e.right.flatMap(a=> f(a).e))
}
def map[B](f:A=>B):Error[E,B]={
Error(e.right.map(f))
}
def toOptionLoggingError():Option[A]={
e.left.map(m => {error(m.toString); m}).right.toOption
}