Created
October 8, 2015 20:05
-
-
Save bananu7/32f388d1fef51072ff31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- non-lens version (BROKEN) | |
addNativeFunction :: String -> FunctionData -> Context -> Context | |
addNativeFunction name fn ctx = newCtx | |
where | |
newRef :: FunctionRef | |
newRef = (\(FunctionRef i) -> FunctionRef (i+1)) $ fst $ Map.findMax (functions ctx) | |
ctxWithNewRef = ctx { functions = Map.insert newRef fn (functions ctx) } | |
insertNewValue :: TableData -> TableData | |
insertNewValue (TableData td) = TableData $ Map.insert (Str name) (Function newRef) td | |
newCtx = ctxWithNewRef { tables = Map.adjust insertNewValue (_Gref ctx) (tables ctx) } | |
-- lens version | |
addNativeFunction :: String -> FunctionData -> LuaM () | |
addNativeFunction name fdata = do | |
newRef <- uniqueFunctionRef | |
functions . at newRef .= Just fdata | |
tables . at gRef . at name .= Just newRef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment