Skip to content

Instantly share code, notes, and snippets.

@amonshiz
amonshiz / foldlExample.hs
Created December 29, 2014 14:59
foldl blog example
foldl (\acc x -> acc + x) 0 [1,2,3,4]
-- acc starts at 0, x starts as the first element of the list, so 1
-- 0 + 1 = 1 -> acc = 1, list = [2,3,4]
-- 1 + 2 = 3 -> acc = 3, list = [3,4]
-- 3 + 3 = 6 -> acc = 6, list = [4]
-- 6 + 4 = 10 -> acc = 10
-- therefore, this foldl results in a value of 10
@amonshiz
amonshiz / CountingDNANucleotides2.hs
Last active August 29, 2015 14:12
Solving Counting DNA Nucleotides on Project Rosalind
import Data.Char
import Data.List
data DNANucleotide = A | C | G | T deriving(Show, Eq, Ord, Read)
charToDNANucleotide :: Char -> DNANucleotide
charToDNANucleotide c = read [toUpper c] :: DNANucleotide
mainCountDNANucleotides = do
dna <- getLine
data AssignmentGrade = AssignmentGrade { score :: Int, grade :: Char } deriving(Show, Read, Eq)
instance Ord AssignmentGrade where
compare ag1 ag2 = compare (score ag1) (score ag2)
addFunction :: Int -> Int -> Int
addFunction x1 x2 = x1 + x2
-- addFunction 1 2 ==> 3
let add10 = addFunction 10
-- :t add10
-- add10 :: Int -> Int
-- add10 5 ==> 15
-- add10 100 ==> 110
doMathFunction :: (Int -> Int -> Int) -> Int -> Int -> Int
doMathFunction func x1 x2 = func x1 x2
-- doMathFunction (+) 1 2 ==> 3
-- doMathFunction (*) 4 3 ==> 12
-- doMathFunction (-) 4 3 ==> 1
rnaConverter = foldr (\x acc -> if x == 'T' then 'U':acc else x:acc) []
module Bioinformatics.DNANucleotide (
DNANucleotide (..),
charToDNANucleotide
) where
import Data.Char
import Data.List
data DNANucleotide = A | C | G | T deriving(Show, Eq, Ord, Read)
@amonshiz
amonshiz / SwiftFind.swift
Last active August 29, 2015 14:12
Haskell style find for Swift Sliceable collections.
func find<S : Sliceable>(domain: S, isValue: (S.Generator.Element) -> Bool) -> S.Index? {
return find(domain, domain.startIndex, isValue)
}
func find<S : Sliceable>(domain: S, startingIndex: S.Index, isValue: (S.Generator.Element) -> Bool) -> S.Index? {
if startingIndex == domain.endIndex {
return nil
}
if isValue(domain[startingIndex]) {
return startingIndex
}
let testText = "<a href=\"#\">Something</a>"
if let firstEndIndex = find(testText, ">") {
let testText2 = testText[Range<String.Index>(start: firstEndIndex.successor(), end: testText.endIndex)]
if let secondStartIndex = find(testText2, "<") {
let testText3 = testText2[Range<String.Index>(start: testText2.startIndex, end: secondStartIndex)]
}
}
let startSplits = split(testText, { $0 == "<" })
let strippedValues = map(startSplits) { (s) -> String? in
if let endIndex = find(s, ">") {
return s[Range<String.Index>(start: endIndex.successor(), end: s.endIndex)]
}
return nil
}
let strings = strippedValues.filter({ $0 != nil }).filter({ $0 != "" }).map({ $0! })