Skip to content

Instantly share code, notes, and snippets.

View jroesch's full-sized avatar

Jared Roesch jroesch

View GitHub Profile
sumDouble = sum . map (*2)
sumDouble = foldl (+) 0 . map (*2) -- without sum
@jroesch
jroesch / gist:3473102
Created August 26, 2012 01:56
Full Generic Scala Version
import scala.math.Numeric.Implicits._
def doubleSum[T](s: Seq[T])(implicit n: Numeric[T]) = s.map (n.fromInt(2) * _) reduce (_ + _)
@jroesch
jroesch / gist:3558924
Created August 31, 2012 21:00
Haskell Version
module Diamond where (diamond)
import Data.List
diamond :: Int -> String
diamond h = concat . intersperse "\n" $ diamondTop ++ diamondBottom
where rows n = map row [((n - x) `div` 2, x) | x <- [1..n], odd x]
diamondTop = rows h
diamondBottom = drop 1 . reverse $ diamondTop
@jroesch
jroesch / diamond.c
Created August 31, 2012 21:07
C Version
int main(){
int inputInt,oddRslt;
char awsr;
int checkNum(int inputInt);
int printDmd(int inputInt);
int repeat(char awsr);
int again=1;
/*main loop, loops entire program, ends on exit */
while(again != 0){
@jroesch
jroesch / gist:3559322
Created August 31, 2012 21:28
Javascript "times"
// Curried aka (Int -> String -> String)
var times = function (n) {
//closure property allows us to access n inside the inner function
return function (x) {
//code isn't executed until all parameters are passed
var array = [];
for (var i in n) {
array[i] = x;
}
return array.reduce(function(a, e) { return a + e; });
@jroesch
jroesch / ConfigParser.scala
Created September 7, 2012 22:52
An implementation of a simple config parser that tries to implement a Dynamic(ish) Object
import scala.collection.mutable.{ Map => MMap }
import scala.util.parsing.combinator._
class Config(s: Seq[(String, Any)] = Seq()) {
val config = MMap[String, Any](s: _*)
def apply(x: String) = config(x)
}
class ConfigError(message: String) extends Exception(message) {
override def toString = "Config Error: " + message
@jroesch
jroesch / gist:3683227
Created September 9, 2012 07:42
toBool
/* Use this as our base */
(.:?) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
obj .:? key = case M.lookup key obj of
Nothing -> pure Nothing
Just v -> parseJSON v
/* I'll look into it tomorrow */
@jroesch
jroesch / gist:3731547
Created September 16, 2012 08:18
Static Import Scala
import java.io.File
import scala.io.Source
object CoffeeImport extends App {
try {
val result = args.toList match {
case Nil => throw new Exception("Please provide a filename: coffee-import <filename>")
case file :: Nil => resolve(file)
}
print(result)
@jroesch
jroesch / pizza.hs
Created October 13, 2012 01:12 — forked from andrewberls/pizza.rb
ACM Micro-Challenge 1
-- JRo
nPizza p s t = ceiling $ (/ s) $ if t `elem` [11..23] then p * 2 else p
-- dumb tests runTests will tell you weather you pass or fail all tests, could do a fold and keep the state of which failed.
between12am11am = 2 == (nPizza 10 5 0) && 1 == nPizza 10 10 1 && 1 == nPizza 7 12 10
between11am11pm = 4 == (nPizza 20 10 11) && 2 == (nPizza 7 12 15)
after11pm = 1 == nPizza 5 10 23
@jroesch
jroesch / SleepyTime.hs
Created November 5, 2012 09:10
For when Chrome gets cranky and takes all your memory ...
module SleepyTime where
-- Check out System.Proccess to avoid using the tmp file.
import System.Cmd
import System.Exit (ExitCode(..))
-- Find something lighter weight for the filtering.
import Text.Regex.TDFA
main :: IO ()
main = do