elixir - Case match on integer range -


is there way match on integer range? looking strip characters after number of characters, , add ellipsis. want do, doesn't match on 1..32.

def cutoff(title)     case byte_size(title)       _ -> title       1..32 -> string.slice(title, 1..32) <> " ..."     end end 

there 2 issues here:

  1. when pattern matching in elixir (and erlang) patterns evaluated top bottom. in case, have catch clause (the ignored variable _) above number range.
  2. you checking against value range 1..32 - byte_size won't return range , return integer. if want check within range must use guard.

if swap order of matches , use guard work:

def cutoff(title)     case byte_size(title)       x when x in 1..32 -> string.slice(title, 1..32) <> " ..."       _ -> title     end end 

you may want slice 0 instead of 1 first character doesn't cut off.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -