Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
nsfyn55 / bubble-sort.scala
Created September 20, 2012 18:09
Bubble Sort
object App{
def main(args: Array[String]){
println(sort(List(6,1,56,3,5,4,9)))
}
def sort(xs:List[Int]):List[Int] = {
def innerSort(xs: List[Int], count:Int): List[Int] = count match {
case 0 => xs
case _ => innerSort(pass(xs), count-1)
@nsfyn55
nsfyn55 / Fizzbuzz.scala
Created October 4, 2012 22:06
Fizzbuzz
object FizzBuzzClient{
def main(args: Array[String]){
val fizzbuzz = for(i <- (_:Range)) yield {
i match {
case i if(i % 15 == 0) => "fizzbuzz"
case i if(i % 3 == 0) => "fizz"
case i if(i % 5 == 0) => "buzz"
case i if(i % 7 == 0) => "bazz"
@nsfyn55
nsfyn55 / reverse.py
Last active October 17, 2018 22:09
Reverse Singly Linked List Python
class Node:
def __init__(self,val,nxt):
self.val = val
self.nxt = nxt
def prnt(n):
nxt = n.nxt
print n.val
if(nxt is not None):
prnt(nxt)
@nsfyn55
nsfyn55 / loan-pattern.scala
Created October 9, 2012 15:36
Annotated Float Loan Pattern
/**
* Functions that do the flattening
*/
object Loans {
//def loan[T1, T2](f: (T1, T2) ⇒ Unit)(implicit f1: (T1 ⇒ Unit) ⇒ Unit, f2: (T2 ⇒ Unit) ⇒ Unit)
// = f1 { a ⇒ f2 { b ⇒ f(a, b) } }
/*
First round of evaluation takes 3 types
@nsfyn55
nsfyn55 / reverse.c
Created October 15, 2012 00:50
Reverse Singly Linked List C
# include <stdio.h>
typedef struct Node {
struct Node *next; //stores the pointer to next
int value;
} Node;
void print_list(Node *root){
while(root){
printf("%d\n", root->value);
@nsfyn55
nsfyn55 / pandigit.scala
Created November 5, 2012 23:42
Smallest Pandigital
object App {
def main(args: Array[String]){
val res = for(i <- 123 to 987 if isPanDigital(split(i));
k <- 1000-i to 987
if isPanDigital(split(i)++split(k)++split(i+k)))yield((i,k))
println(res)
}
def isPanDigital(ls: List[Int]) = {
@nsfyn55
nsfyn55 / appfunc.scala
Created November 24, 2012 04:52
Example Applicative Functor
/*
Functor:
fmap:
Takes a set and changes it to another set
signature:
(f: A => B): F[A] => F[B]
point:
lifts type to functor type
signature:
@nsfyn55
nsfyn55 / contravariance.scala
Last active October 13, 2015 04:38
Contravariance Example
/*
Invariant:
String is subclass of Object but in java
List<String> is not a subclass of List<Object>
Covariant
String in scala is a subclass of Object
List[String] is a subclass of List[Object]
@nsfyn55
nsfyn55 / byname.scala
Created November 26, 2012 19:09
By Name Parameter
/*
a by name parameter is not evaluated at the point of function application
but rather it is evaluated at each use within the function
*/
object App {
def main (args: Array[String]){
println(delayed(nano()))
}
def nano() ={
@nsfyn55
nsfyn55 / compose.scala
Created November 29, 2012 18:47
Composable Functions
object App {
def main (args: Array[String]){
val fp = candidate(_)
val f = decorate(fp,decorator) // F o G
println(f("Hello World"))
println(fp("Hello World"))