Skip to content

Instantly share code, notes, and snippets.

View pjrt's full-sized avatar
🤔
Thinking about why Github now has statuses

Pedro Rodriguez Tavarez pjrt

🤔
Thinking about why Github now has statuses
View GitHub Profile
import Data.List (intersperse, foldl')
import Data.List.Split (chunksOf)
numbers :: Int -> String
numbers c =
case c of
0 -> ""
1 -> "one"
2 -> "two"
@pjrt
pjrt / transitive.hs
Last active March 4, 2019 16:29
Transitive typeclass instances in Haskell
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
{-# LANGUAGE FunctionalDependencies, TypeApplications #-}
import Data.Proxy
data User
class MyClass a b | b -> a
instance MyClass Char Int
@pjrt
pjrt / sendmail_android.py
Created December 6, 2017 20:06
A script to send emails out of Tasker. The script is just an updated version of http://tasker.wikidot.com/sendemail.
#!/usr/bin/python
# sendemail.pl - Sends email based on a conf file or arguments
# DOCUMENTATION:
# USAGE:
# Either:
# sendemail.py
# - Checks /mnt/sdcard/tmp/mailout.conf by default
# sendemail.py <conf file location>
# - Specify a conf file location
@pjrt
pjrt / TCBox.scala
Last active October 2, 2020 10:24
A typeclass box: example usage of Existential Types in Scala.
import scala.language.implicitConversions
import scala.language.higherKinds
/**
* A simple typeclass that converts types A to some specific Java type
*/
trait AllowedType[A] {
type JavaType >: Null <: AnyRef
@pjrt
pjrt / PartialFunc.hs
Last active February 15, 2017 20:37
Partial Functions in Haskell
{-# LANGUAGE TypeOperators #-}
import Data.Maybe
import Control.Applicative
import Control.Category
import Prelude hiding ((.))
data a ~> b =
Partial (a -> Maybe b) -- a partial function
| Defaulted (a ~> b) b -- a partial function with a default value
@pjrt
pjrt / qsbt.hs
Created December 17, 2016 23:38
Quickfix SBT
#!/usr/bin/env bash
############
# QuickSBT #
############
# Launch SBT with support for generating /tmp/sbt.quickfix file for Vim
# http://github.com/aloiscochard / https://gist.github.com/4698501
gitroot=$(git rev-parse --show-toplevel);\
countChg :: Int -> [Int] -> Int
countChg 0 _ = 1 -- There is only one way of counting an amount of 0
countChg _ [] = 0 -- There are 0 ways to count for no coins
countChg amt coins@(c:cs)
| amt < 0 = 0 -- This branch failed, we got something less than 0
| otherwise = countChg amt cs + countChg (amt - c) coins -- Branch out into a branch with one less coin and
-- a branch with the same number of coins but one coin
-- reduced from the amount
trait TypeclassA[A] { }
trait TypeclassB[A] { }
trait TypeclassC[A] {
implicit val typeAInstance: TypeclassA[A]
implicit val typeBInstance: TypeclassB[A]
}
object TypeclassC {
findLowest :: [Int] -> [Int] -> Int
findLowest as bs = findLowest' (sort as) (sort bs)
where
findLowest' _ [] = -1
findLowest' [] _ = -1
findLowest' (a:ta) (b:tb)
| a == b = a
| b < a = findLowest' (a:ta) (tb)
| otherwise = findLowest' ta (b:tb)
{-# LANGUAGE MultiWayIf #-}
-- http://codercareer.blogspot.com/2013/03/no-46-nodes-with-sum-in-binary-search.html
data Tree a = Leaf a | Branch a (Tree a) (Tree a)
deriving Show
getValue (Leaf v) = v
getValue (Branch v _ _) = v