Skip to content

Instantly share code, notes, and snippets.

-- stack --system-ghc runghc
{- |
Result of pair programming with [bumbleblym](https://github.com/bumbleblym)
He implemented most of the code below and suggested using parsers from base
rather than @getLine@ & @read@ to parse the input.
This increases code modularity and ease of refactoring.
Interesting parts:
@kwannoel
kwannoel / use-box.dump
Last active July 31, 2020 09:08
Usecase for Rc in Raytracers
error[E0382]: use of moved value: `metal_shiny`
--> temp.rs:39:38
|
37 | let metal_shiny = Metal::new(0.0);
| ----------- move occurs because `metal_shiny` has type `Metal`, which does not implement the `Copy` trait
38 | let sphere1 = Sphere::new(Box::new(metal_shiny));
| ----------- value moved here
39 | let sphere2 = Sphere::new(Box::new(metal_shiny));
| ^^^^^^^^^^^ value used here after move
@kwannoel
kwannoel / yy.hs
Created October 13, 2020 12:20
yy.hs
{- |
REFERENCES:
https://gist.github.com/lukechampine/a3956a840c603878fd9f
https://rosettacode.org/wiki/Y_combinator#Haskell
https://mvanier.livejournal.com/2897.html
GOAL: Implement recursion in languages which do not support **Explicit recursion**
Do so in an intuitive way
PREREQUISITES:
{- |
DESCRIPTION
This program parses valid NRICs from text.
NRICs are unique identification numbers given to Singaporeans and Permanent residents of Singapore.
SYSTEM
This runs on GHC 8.10.1
These libraries should be included with your GHC distribution:
@kwannoel
kwannoel / benchmark-async.hs
Last active December 9, 2020 04:00
Benchmarking async
#!/usr/bin/env stack
-- stack exec ghc --resolver lts-16.2 --package async --package criterion -- -threaded -O2 -rtsopts -with-rtsopts=-N
import Control.Concurrent.Async (async, mapConcurrently_)
import Control.Monad (replicateM_, void)
import Criterion.Main
main :: IO ()
main = defaultMain
#!/usr/bin/env stack
-- stack script --resolver lts-16.2 --package criterion --package relude --package text --package deepseq
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.DeepSeq (NFData)
import Control.Monad (replicateM_, void)
import Criterion.Main
#!/usr/bin/env stack
-- stack script --resolver lts-16.2 --package criterion
{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (replicateM_, void)
import Criterion.Main
import Data.Foldable (foldl', foldr)
main :: IO ()
#!/usr/bin/env stack
-- stack exec ghc --resolver lts-16.2 --package time --package async --package text --package deepseq -- -prof -fprof-auto -threaded -O2 -rtsopts -with-rtsopts=-N -eventlog
{-
See if we can reproduce having to repeatedly generate environments
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RecordWildCards #-}
@kwannoel
kwannoel / bf-fold-tree-strict.hs
Created January 4, 2021 06:38
BFS tree fold
#!/usr/bin/env stack
-- stack exec ghc --resolver lts-16.2 --package criterion -- -threaded -O2 -rtsopts -with-rtsopts=-N -eventlog
import Control.Monad (replicateM_, void)
import Criterion.Main
import Data.Foldable (foldl')
import Data.Tree
treeFoldStrict :: (b -> [a] -> b) -> b -> Tree a -> b
treeFoldStrict f init (Node val children) =
a a a a a a