Re: E Style

"Mark S. Miller" <[email protected]>
Newsgroups gmane.comp.lang.e.general
Message-ID <[email protected]>
On Sun, Dec 5, 2010 at 12:23 PM, Bill Frantz <[email protected]> wrote:

> In trying to write an E program (to actually do something useful
> to me), I noted the apparent lack of the C construct:
>
>   (bool ? true_value : false_value)
>
> What I have working now is:
>
>   def location := ( if (filename =~ `@{x}Cave@{y}`)   { 0 }
> else if \
>                        (filename =~ `@{x}Frosty@{y}`) { 1 }
> else if \
>                        (filename =~ `@{x}TTT@{y}`)    { 2 }
> else    \
>                        { -1 })
>   if (location == -1) {throw(`ERROR: Filename "$filename"
> yields $location for offset`)}
>
> (The backslashes seem to be needed.)
>

I like Thomas' switch suggestion better than the chained if. But just to
show how to avoid the backslashes, I might format your original as:

  def location := ( if (filename =~ `@{x}Cave@{y}`)   {
      0
  } else if (filename =~ `@{x}Frosty@{y}`) {
      1
  } else if (filename =~ `@{x}TTT@{y}`)    {
      2
  } else { -1 })

or possibly

  def location := ( if (filename =~ `@{x}Cave@{y}`)   {
      0 } else if (filename =~ `@{x}Frosty@{y}`) {
      1 } else if (filename =~ `@{x}TTT@{y}`)    {
      2 } else { -1 })

or

  def location := ( if (filename =~ `@{x}Cave@{y}`)   { 0
  } else if (filename =~ `@{x}Frosty@{y}`) { 1
  } else if (filename =~ `@{x}TTT@{y}`)    { 2
  } else { -1 })

Of these, I'd prefer the first even though it's wasteful of vertical space.


>
> Is there a cleaner way to code this fragment?
>

Independent of chained ifs vs switches, since you are not using the portions
of the specimen matched against the variables in the patterns, I would use
@{_} in the patterns for clarity (and btw an extra smidgen of efficiency).
In E, "_" is the ignore pattern -- matches everything and binds nothing.



>
> Cheers - Bill
>
> -----------------------------------------------------------------------
> Bill Frantz        | I like the farmers' market   | Periwinkle
> (408)356-8506      | because I can get fruits and | 16345
> Englewood Ave
> www.pwpconsult.com | vegetables without stickers. | Los Gatos,
> CA 95032
>
>
> _______________________________________________
> e-lang mailing list
> [email protected]
> http://www.eros-os.org/mailman/listinfo/e-lang
>



-- 
    Cheers,
    --MarkM

_______________________________________________
e-lang mailing list
[email protected]
http://www.eros-os.org/mailman/listinfo/e-lang
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.