Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
nsfyn55 / offset.py
Created May 21, 2012 21:34
Timezone Hours Offset
import pytz
from pytz import timezone
from datetime import datetime,timedelta
import time
utc = pytz.utc
utc.zone
eastern = timezone("America/New_York")
loc_dt = eastern.localize(datetime.now())
offset = loc_dt.utcoffset()
@nsfyn55
nsfyn55 / Palindrome.scala
Created June 11, 2012 15:39
Scala Palindrome Checker
object App{
def isPalindrome(s: String) = {
(for (x <- 0 to s.length/2) yield (s(x) == s(s.length - x - 1)))
.reduceLeft((acc,n)=> acc && n)
}
}
@nsfyn55
nsfyn55 / palidrome.py
Created June 11, 2012 20:08
Palidrome checker python
def isPalindrome(s):
return s[0:len(s)/2] == s[len(s) - len(s)/2:len(s)][::-1]
isPalindrome('foof') #true
isPalindrome('fooof') #true
isPalindrome('fooob') #false
isPalindrome('foob') #false
@nsfyn55
nsfyn55 / ReverseList.scala
Created June 18, 2012 18:21
Reverse Singly Linked List
def reverse[T](xs: List[T]): List[T] = xs match {
case List() => List()
case x :: xs1 => reverse(xs1) ::: List(x)
}
@nsfyn55
nsfyn55 / FunctionalQueue.scala
Created June 19, 2012 17:57
Purely Functional Queue
class Queue[T](
private val leading: List[T],
private val trailing: List[T]
) {
/*
if leading.isEmpty return a new Queue
where trailing is reversed and leading
is Nil
@nsfyn55
nsfyn55 / Maybe.scala
Created August 5, 2012 17:45
Option(Maybe) Monad
object Monad2{
/*
Imagine you have a string name and you want to do a bunch of operations on it
but you want to program defensively. In Java this would be implemented as
a number of tiered null checks. Scala's Option Monad allows us to do this
much more elegantly
1. Option can be one of two states Some or None. None covers any states that
are not Some in this case ("" , null, Null, Nothing, None"). In pure math terms
this is called a Monadic Zero. Operations on Monadic Zero have pretty much
@nsfyn55
nsfyn55 / bin-search.scala
Created August 22, 2012 15:44
Scala Binary Search
import scala.annotation.tailrec
object BinarySearchApp{
def main(args: Array[String]){
val l = List(1,2,4,5,6, 8,9,25,31);
println("Hello World!")
println(search2(5, l))
println(search2(6, l))
println(search2(7, l))
}
@nsfyn55
nsfyn55 / inplace-reverse.java
Created August 25, 2012 21:03
In Place Array Reverse
package com.art.samples.inplace_string_reverse;
/**
* Hello world!
*
*/
public class App
{
/**
* @param args
*/
@nsfyn55
nsfyn55 / cakepatter.scala
Created September 13, 2012 21:05
Cake Pattern
/*
Article: http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/
The cake pattern is a way to do dependency injection in Scala.
Rather than using explicit xml configuration we compose our
configurations using a combination of self-typ annotations, traits,
and companion objects.
self-type Annotation
====================
@nsfyn55
nsfyn55 / variance.scala
Created September 15, 2012 19:06
Scala Variance
/*
Assignment compatibilty has several dimensions. The object type and the
type of its parameters. Type parameters can be covariant the object type
cannot. What's the difference? Covariant parameters allow subclassing.
Defintions:
==========
covariant: converting from wider(Animals) to narrower(Cats)
contravariant: converting from narrower to wider Triangles->shapes
invariant: not able to convert