Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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.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 / 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 / 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 / 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
@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 / 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
*/