lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Sean Conner once stated:
> It was thus said that the Great Soni L. once stated:
> > (Copypasted from 
> > https://gist.github.com/SoniEx2/99882fa8d5a0740339b0#the-orif-idea )
> > 
> > The orif Idea
> > =============
> > 
> > orif (possible operators: `|if`, `orif`, `or if`, ...) is a construct that 
> > allows if statements with **explicit** fall-through.
> 
>   It's an interesting idea, but I'm still unsure if I like the name "orif"
> or not.  I don't really like it, but I can't come up with a better name.
> 
>   But, if it's a switch with a fallthrough case you are missing (and
> frankly, I really haven't missed it that much in Lua), what about the
> following?
> 
> 	-- ----------------------------
> 	-- You need this function
> 	-- ----------------------------
> 
> 	function switch(key,case,otherwise)
> 	  otherwise = otherwise or function() return nil end
> 	  
> 	  if not case[key] then
> 	    return otherwise(case,key)
> 	  else
> 	    return case[key](case,key)
> 	  end
> 	end

  Here's a better (smaller, faster) function that does the same thing:

	-- --------------------------------------------------
	-- License: GPL, LGPL, MIT, BSD.  Pick your poison.
	-- --------------------------------------------------

	function switch(key,case,otherwise)
	  return (key[case] or otherwise or function() return nil end)(case,key)
	end

  -spc