Skip to content

Instantly share code, notes, and snippets.

@ssanj
ssanj / gist:5caaffbf7eb6ba76394f
Created April 30, 2015 00:46
Swapping containers while retain the values inside them
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.language.{higherKinds, postfixOps}
import scala.util.{Failure, Success}
object ContainerMapper {
import scala.concurrent.ExecutionContext.Implicits.global
//this is your typeclass signature. M[_] means M is a type 'constructor'. It needs another type (A) to become a type. List[_] vs List[A].
object AmTest extends App {
val x = List("=",
"+",
"==",
"applyTo",
"asInstanceOf",
"attr",
"charAt",
"chars",
@ssanj
ssanj / Functor.scala
Created February 16, 2011 22:56
Functor definition
List("one", "two", "three") map (_.length)
trait Functor[F[_]] {
def fmap[A,B](f: A => B, fa:F[A]): F[B]
}
class ListFunctor extends Functor[List] {
def fmap[A,B](f: A => B, fa:List[A]): List[B] = fa match {
case Nil => Nil
case x::xs => f(x) :: fmap(f, xs)
case class Person(name:String)
fmap((_:Person).name, Holder(Person("Harry Potter")))
11: error: could not find implicit value for parameter functor: Functor[Holder]
List -> is a type constructor
List[Long] -> is a type
@ssanj
ssanj / Unsafe List
Created March 31, 2011 01:56
Adding any values to a typed List
scala> val ar:List[Int] = Nil
ar: List[Int] = List()
scala> "blah" :: 5 :: 6 :: ar
res21: List[Any] = List(blah, 5, 6)
//how did I add a String to a List[Int]?
//how do you keep the type within the List fixed at Int?
//can I only do that at declaration time as opposed to when I call the :: method?
//This works:
@ssanj
ssanj / gist:905263
Created April 6, 2011 07:14
emacs bindings for scala
;; Load the ensime lisp code...
(add-to-list 'load-path "ENSIME_ROOT/elisp/")
(require 'ensime)
;; This step causes the ensime-mode to be started whenever
;; scala-mode is started for a buffer. You may have to customize this step
;; if you're not using the standard scala mode.
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
;; MINI HOWTO:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
@ssanj
ssanj / add_iptable_rule.sh
Created November 22, 2011 22:33
iptable_rules
sudo iptables -A CHAIN_NAME
sudo chmod +x /etc/network/if-pre-up.d/iptablesload