Skip to content

Instantly share code, notes, and snippets.

@diegonc
diegonc / ordered-encode.lua
Created September 13, 2018 18:36
lua-protobuf: encode fields in sequential order
local function pbOrderedEncode(type, data)
-- Check type
if pb.type(type) == nil then
return nil, string.format("type '%s' does not exists", type)
end
-- List type fields
local typeFields = {}
for name, number, type, defval, flags in pb.fields(type) do
typeFields[name] = {
name = name,
@diegonc
diegonc / CODE_GOLF_A_REDUCER.md
Last active December 18, 2016 04:22
[Elm] Have some fun reducing a list while unwrapping a Result

Say there's an l of type List a, an e of type b which represent an empty collection and a function add of type a -> b -> Result x b.

add inserts an a into a b which is an operation that could fail.

I'd like to apply a fold through l using add as the reducer so that in the end I have a Result with either the b that contains all of the as in the list or an error.

However, I can't just do List.foldl add e l because of the signature of add.