can this be made shorter?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Emmanuel Touzery
hi,

 i want to know if a string contains only "." and "/" characters.

 i do something like that:

null . dropWhile (\c -> c == '.' || c == '/')

  now it's a shame, because if I wanted only to check for "." then I would
have:

null . dropWhile (=='.')

   afaik in scala you can say:

null . dropWhile (_ == '.' || _ == '/')

   which is a bit more compact than the haskell...

   I was thinking to use "and" but I'm not sure it would end up being
readable...

   Any idea? Or I am trying too hard to make it compact?

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130529/888ba3cb/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Felipe Lessa
Well, you could use:

    null . dropWhile (`elem` "./")

=)

On Wed, May 29, 2013 at 4:38 PM, Emmanuel Touzery <etouzery at gmail.com> wrote:

> hi,
>
>  i want to know if a string contains only "." and "/" characters.
>
>  i do something like that:
>
> null . dropWhile (\c -> c == '.' || c == '/')
>
>   now it's a shame, because if I wanted only to check for "." then I would
> have:
>
> null . dropWhile (=='.')
>
>    afaik in scala you can say:
>
> null . dropWhile (_ == '.' || _ == '/')
>
>    which is a bit more compact than the haskell...
>
>    I was thinking to use "and" but I'm not sure it would end up being
> readable...
>
>    Any idea? Or I am trying too hard to make it compact?
>
> Emmanuel
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



--
Felipe.


Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Emmanuel Touzery
Thank you, that's exactly what I was looking for!
I didn't spot "elem" so far... that's why I was looking into "and" which
was too complex for this case.

Thanks again!

emmanuel


On Wed, May 29, 2013 at 9:44 PM, Felipe Almeida Lessa <
felipe.lessa at gmail.com> wrote:

> Well, you could use:
>
>     null . dropWhile (`elem` "./")
>
> =)
>
> On Wed, May 29, 2013 at 4:38 PM, Emmanuel Touzery <etouzery at gmail.com>
> wrote:
> > hi,
> >
> >  i want to know if a string contains only "." and "/" characters.
> >
> >  i do something like that:
> >
> > null . dropWhile (\c -> c == '.' || c == '/')
> >
> >   now it's a shame, because if I wanted only to check for "." then I
> would
> > have:
> >
> > null . dropWhile (=='.')
> >
> >    afaik in scala you can say:
> >
> > null . dropWhile (_ == '.' || _ == '/')
> >
> >    which is a bit more compact than the haskell...
> >
> >    I was thinking to use "and" but I'm not sure it would end up being
> > readable...
> >
> >    Any idea? Or I am trying too hard to make it compact?
> >
> > Emmanuel
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
>
>
> --
> Felipe.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130529/afae668d/attachment-0001.htm>

Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Tilmann
you might want to use 'all' as well:

Prelude> (all $ flip elem "./") "...."
True


Am 29.05.2013 21:50, schrieb Emmanuel Touzery:
> null . dropWhile (`elem` "./")



Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Emmanuel Touzery
ah, i wrote "and" but i meant "all" in my emails ;-)

but yes i thought about all in combination with the null, to go over "."
and "/".

yes this is clearer, just too bad about the flip.


On Wed, May 29, 2013 at 10:00 PM, Tilmann <t_gass at gmx.de> wrote:

> you might want to use 'all' as well:
>
> Prelude> (all $ flip elem "./") "...."
> True
>
>
> Am 29.05.2013 21:50, schrieb Emmanuel Touzery:
>
>  null . dropWhile (`elem` "./")
>>
>
>
> ______________________________**_________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130529/9943ead8/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Henk-Jan van Tuyl
In reply to this post by Tilmann
On Wed, 29 May 2013 22:00:58 +0200, Tilmann <t_gass at gmx.de> wrote:

> you might want to use 'all' as well:
>
> Prelude> (all $ flip elem "./") "...."
> True

or:
   all (`elem` "./") "...."

Regards,
Henk-Jan van Tuyl


--
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


Reply | Threaded
Open this post in threaded view
|

can this be made shorter?

Sylvain HENRY
In reply to this post by Emmanuel Touzery
>    afaik in scala you can say:
>
> null . dropWhile (_ == '.' || _ == '/')
>
>    which is a bit more compact than the haskell...

No, it would be desugared to:
dropWhile ((a,b) => a == '.' || b == '/')

scala> "abc".dropWhile(_ == '.' || _ == '/')
<console>:8: error: wrong number of parameters; expected = 1
               "abc".dropWhile(_ == '.' || _ == '/')

You would have to do the same thing as in Haskell to make it work:

scala> "abc".dropWhile(a => a == '.' || a == '/')
res20: String = abc

Cheers
Sylvain