Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
[Coffee] -> IO Code

Ashesh Ambasta asheshambasta

💭
[Coffee] -> IO Code
View GitHub Profile
View AuthenticationSample.scala
// authenticate a user with a email + password
trait Authenticable {
def email: String
def password: String
def auth: Option[User] = dbAuthenticate(email, password) // dbAuthenticate is an example method that handles the authentication using a secure DB
def ~ = auth
}
@asheshambasta
asheshambasta / transposeText.hs
Last active Jan 23, 2016
Transpose a chunk of text in Haskell
View transposeText.hs
-- transpose a chunk of text separated by '\n'
transposeText :: String -> String
transposeText text = unlines (transpose' (lines text))
transpose' :: [String] -> [String]
transpose' sentences
| not (all null sentences) = currentLine : (transpose' (map tail nonEmptySts))
| otherwise = []
where currentLine = [x | x:_ <- nonEmptySts]
View lazyRead.hs
import Data.Char(toUpper)
main = interact (map toUpper)
View return2.hs
--file: ch07/return2.hs
import Data.Char(toUpper)
isYes :: String -> Bool
isYes str = (toUpper . head $ str) == 'Y'
isGreen :: IO Bool
isGreen = putStrLn "green your favourite colour dawg?" >>
getLine >>= (\str -> return (isYes str))
@asheshambasta
asheshambasta / JsonFlatten.scala
Created Feb 13, 2017
Very simple + untidy json flattener.
View JsonFlatten.scala
import play.api.libs.json.{JsObject, JsValue, Json}
implicit class JsonFlatten(v: JsValue) {
def toMap(pref: String = ""): Map[String, String] = if (v.isIterable) v.validate[Seq[JsObject]] map { jsons =>
jsons.foldLeft(Map.empty[String, String]) {
case (acc, json) => acc ++ json.toMap(pref)
}
} getOrElse Map.empty[String, String] else v.validate[Map[String, JsValue]] map { mp =>
mp.foldLeft(Map.empty[String, String]) {
@asheshambasta
asheshambasta / stack-out.log
Created Feb 4, 2018
error during `stack install gtk`
View stack-out.log
er.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Special/Ruler.o )
[ 39 of 209] Compiling Graphics.UI.Gtk.Gdk.DisplayManager ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Gdk/DisplayManager.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Gdk/DisplayManager.o )
[ 40 of 209] Compiling Graphics.UI.Gtk.Entry.EntryBuffer ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Entry/EntryBuffer.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Entry/EntryBuffer.o )
[ 41 of 209] Compiling Graphics.UI.Gtk.Entry.Editable ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Entry/Editable.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Entry/Editable.o )
[ 42 of 209] Compiling Graphics.UI.Gtk.Embedding.Types ( .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Embedding/Types.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Graphics/UI/Gtk/Embedding/Types.
View LookupDNS.hs
-- stack ghci --package dns --package bytestring
-- import Network.DNS.Resolver
-- import Network.DNS.Lookup
-- import qualified Data.ByteString.Char8 as BSC
-- reads newline separated domain names and dumps their A records to an output file.
readFile "/tmp/domains.txt"
>>= return . map BSC.pack . lines
@asheshambasta
asheshambasta / stack.yaml
Created Aug 28, 2019
composite git hashes might need to be modified.
View stack.yaml
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#