Skip to content

Instantly share code, notes, and snippets.

View codedmart's full-sized avatar

Brandon Martin codedmart

View GitHub Profile
@codedmart
codedmart / functions.go
Last active August 29, 2015 14:04
Functional Randomness with Languages
package main
import "fmt"
type Lx func(int) int
type Fx func(int) Lx
type Mx func(int) Fx
func cmbSum(a int, b int) Mx {
return func(c int) Fx {
verify j = case J.verify (J.secret $ pack "secret-key") =<< J.decode j of
Nothing -> return Nothing
Just j -> do
r <- expired j
case r of
Just True -> return $ Just j
_ -> return Nothing
create d t = do
r <- runDB $ t R.# R.insert (R.toDatum d)
return . maybeHelper $ insertKey r
import System.Random
-- takes a random generator and returns a list of strings of 50 chars
start_population :: (RandomGen g) => g -> [[Char]]
start_population gen =
[take 50 $ randomRs ('A', 'z') gen | x <- [0..]]
main = do
randomGen <- newStdGen -- get a random generator
instance ToDatum (Ratio Integer) where
toDatum a = toDatum (parseRational a)
parseRational :: Rational -> Double
parseRational = fromRational
@codedmart
codedmart / line.hs
Created November 5, 2014 18:13
Possible solution for run h $ line [[1, 2],[3,4],[5,6]] :: IO Line
-- Looks like run h $ line [[1,2],[3,4],[5,6]] :: IO Line was not matching on Array v so was falling to mempty
-- This solution return (in Show form) fromList [LonLat 1 2,LonLat 3 4,LonLat 5 6]
instance FromDatum a => FromDatum (Vector a) where
parseDatum (Array v) = fmap V.fromList . mapM parseDatum $ V.toList v
parseDatum (Line l) = fmap V.fromList . mapM (parseDatum . toDatum) $ V.toList l
parseDatum _ = mempty
### Failure in ./Database/RethinkDB/Functions.hs:409: expression `run' h $ table "posts" # group (!"author") (reduce (\a b -> a + "\n" + b) . R.map (!"message"))'
expected: [{"group":"bill","reduction":"hi\nhello"},{"group":"bob","reduction":"lorem ipsum"}]
but got: [{"group":"bill","reduction":"hello\nhi"},{"group":"bob","reduction":"lorem ipsum"}]
diff --git a/Database/RethinkDB/Network.hs b/Database/RethinkDB/Network.hs
index 74cb1d9..feec063 100644
--- a/Database/RethinkDB/Network.hs
+++ b/Database/RethinkDB/Network.hs
@@ -28,10 +28,10 @@ import Control.Monad (when, forever, forM_)
import Data.Typeable (Typeable)
import Network (HostName)
import Network.Socket (
- socket, Family(AF_INET), SocketType(Stream), sClose, SockAddr(SockAddrInet), setSocketOption, SocketOption(NoDelay),
- Socket)
instance FromDatum a => Result (Cursor a) where
convertResult r = do
(v, _) <- convertResult r
(_, p) <- responseProfile v
c <- makeCursor r
return (c { cursorMap = unsafeFromDatum }, p)
weak <- mkWeakPtr mbox $ Just $ do
closeToken h tok -- TODO: don't close if already closed
atomicModifyIORef' (rdbWait h) $ \mboxes ->
(M.delete tok mboxes, ())