Skip to content

Instantly share code, notes, and snippets.

@mightybyte
mightybyte / DynListDemo.hs
Created June 14, 2016 05:05
DynamicList Demo
-- Paste this into http://hsnippet.com after adding Reflex.Dom.Contrib.Widgets.DynamicList
-- to the imports list in HSnippet's imports tab.
app :: MonadWidget t m => App t m ()
app = do
addItem <- button "Add Item"
dynamicList single snd (const never) ("edit me" <$ addItem)
["alpha", "bravo", "charlie"]
return ()
data CurAppState = Login | Home | Other
app :: MonadWidget t m => CurAppState -> m ()
app Login = ...
app Home = ...
app Other = ...
navBar :: MonadWidget t m => m (Event t CurAppState)
navBar = do
a <- button "Login"
newtype AceRef = AceRef { unAceRef :: JSVal }
data ACE t = ACE
{ aceRef :: AceRef
, aceValue :: Dynamic t String
}
------------------------------------------------------------------------------
startACE :: String -> IO AceRef
#ifdef ghcjs_HOST_OS
@mightybyte
mightybyte / twitter.sch
Last active March 3, 2016 10:21
Small snippet illustrating the use of http://reply.obsidian.systems
(define-syntax elAttr
(syntax-rules ()
((elAttr tag attrs children ...)
(eval-element 'tag 'attrs 'children ...))))
(define-syntax el
(syntax-rules ()
((el tag children ...)
(elAttr tag () children ...))))
@mightybyte
mightybyte / clock.patch
Created December 18, 2015 00:06
diff -r clock-0.5.1 clock-0.5.2
diff -r clock-0.5.1/System/Clock.hsc clock-0.5.2/System/Clock.hsc
173,177c173,175
< normalize (TimeSpec xs xn)
< | xn < 0 || xn >= 10^9 =
< let (q, r) = xn `divMod` (10^9)
< in TimeSpec (xs + q) r
< | otherwise = TimeSpec xs xn
---
> normalize (TimeSpec xs xn) | xn < 0 || xn >= 10^9 = TimeSpec (xs + q) r
> | otherwise = TimeSpec xs xn
@mightybyte
mightybyte / gist:ae26c9b9d7bef0419b0f
Last active November 13, 2015 18:45
Get all of a function's arguments and return value
getArgs :: Type -> [Type]
getArgs (ForallT _ _ t) = getArgs t
getArgs (SigT t _) = getArgs t
getArgs (AppT (AppT ArrowT a) b) = a : getArgs b
getArgs b = [b]
-- Possible lens version
prismCase [ _ForallT . _3 :-> getArgs, _SigT . _1 :-> getArgs, _AppAppArrow :-> \(a,b) -> a : getArgs b, id :-> \b -> [b] ]
@mightybyte
mightybyte / reflex-hilbert-diagram.hs
Last active February 5, 2016 21:15
Diagrams example snippet
app :: MonadWidget t m => App t m ()
app = do
ti <- textInput $ TextInputConfig "range" "4" never
(constDyn $ "min" =: "1" <> "max" =: "6")
n <- holdDyn (4::Int) (read <$> updated (value ti))
let f = reflexDia (def & sizeSpec .~ D.mkSizeSpec2D (Just 600) (Just 600)) . example
el "div" $ widgetHoldHelper f 4 (updated n)
return ()
hilbert 0 = mempty
@mightybyte
mightybyte / location.hs
Created September 29, 2015 17:40
Reflex Location Management
------------------------------------------------------------------------------
-- | Gets the current path of the DOM Window (i.e., the contents of the
-- address bar after the host, beginning with a "/").
-- https://developer.mozilla.org/en-US/docs/Web/API/Location
getWindowLocationPathname :: DOMWindow -> IO String
#ifdef ghcjs_HOST_OS
getWindowLocationPathname w = do
jw <- toJSRef w
liftM fromJSString $ js_windowLocationPathname jw
@mightybyte
mightybyte / gist:753e2d5dea800b2eb0cc
Last active August 29, 2015 14:27
gen-bounds output
$ dist/build/cabal/cabal gen-bounds
Resolving dependencies...
The following packages need bounds and here is a suggested starting point.
You can copy and paste this into the build-depends section in your .cabal
file and it should work (with the appropriate removal of commas).
Note that version bounds are a statement that you've successfully built and
tested your package and expect it to work with any of the specified package
versions (PROVIDED that those packages continue to conform with the PVP).
@mightybyte
mightybyte / WidgetTypes.hs
Last active May 30, 2017 22:09
FRP Widgets
data WidgetConfig t a
= WidgetConfig { _widgetConfig_setValue :: Event t a
, _widgetConfig_initialValue :: a
, _widgetConfig_attributes :: Dynamic t (Map String String)
}
makeLenses ''WidgetConfig
instance (Reflex t, Default a) => Default (WidgetConfig t a) where
def = WidgetConfig { _widgetConfig_setValue = never