- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Match file extension in file name
Tue, 2011-11-29, 19:54
Point taken. Ahh, if only the regex were simpler...
http://swtch.com/~rsc/regexp/regexp1.html
Peace. Michael
On Tue, Nov 29, 2011 at 10:48 AM, Alan Burlison wrote:
> On 29/11/2011 18:44, Michael Schmitz wrote:
>
>> val pat = """(.*)[.](.*)""".r
>
>
> That will backtrack unnecessarily. """([^.]*)\.([^.]*)""" would be better.
> But of course that still won't deal with missing prefix or suffix parts.
> Ah, the joys of regex :-)
>
> --
> Alan Burlison
> --
Tue, 2011-11-29, 22:17
#2
Re: Match file extension in file name
FYI - ended up with:
for (n <- filenames) """.\w+$""".r findFirstIn n match {
case Some(".xml") => ...
case Some(".rzt") => ...
case Some(_) => ...
case None => ....
}
Thanks again
On Tue, Nov 29, 2011 at 11:23 AM, mark dufresne <mark.dufresne@gmail.com> wrote:
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
for (n <- filenames) """.\w+$""".r findFirstIn n match {
case Some(".xml") => ...
case Some(".rzt") => ...
case Some(_) => ...
case None => ....
}
Thanks again
On Tue, Nov 29, 2011 at 11:23 AM, mark dufresne <mark.dufresne@gmail.com> wrote:
I'm going to play around with: """.ext$"""
thanks guys
On Tue, Nov 29, 2011 at 10:53 AM, Michael Schmitz <michael@schmitztech.com> wrote:
Point taken. Ahh, if only the regex were simpler...
http://swtch.com/~rsc/regexp/regexp1.html
Peace. Michael
On Tue, Nov 29, 2011 at 10:48 AM, Alan Burlison <alan.burlison@gmail.com> wrote:
> On 29/11/2011 18:44, Michael Schmitz wrote:
>
>> val pat = """(.*)[.](.*)""".r
>
>
> That will backtrack unnecessarily. """([^.]*)\.([^.]*)""" would be better.
> But of course that still won't deal with missing prefix or suffix parts.
> Ah, the joys of regex :-)
>
> --
> Alan Burlison
> --
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
Wed, 2011-11-30, 04:27
#3
Re: Match file extension in file name
drop actually handles that, see my second post :)
file.getName.drop(file.getName lastIndexOf '.') match { ...}
On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
file.getName.drop(file.getName lastIndexOf '.') match { ...}
On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Neither of you handled lastIndexOf(".") == -1.Also will lastIndexOf('.') be handled by java.lang.String#lastIndexOf(Int) or by the implicit to Iterable[Char]?
On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison <alan.burlison@gmail.com> wrote:On 29/11/2011 21:15, Josh Suereth wrote:
file.getName.dropWhile(_ != '.') match {
case ".exe" => ...
case _ => ???
}
str.substring(str.lastIndexOf(".") + 1)
Wed, 2011-11-30, 06:17
#4
Re: Match file extension in file name
Good exercise to learn to use zippers.
drop actually handles that, see my second post :)
file.getName.drop(file.getName lastIndexOf '.') match { ...}
On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Neither of you handled lastIndexOf(".") == -1.Also will lastIndexOf('.') be handled by java.lang.String#lastIndexOf(Int) or by the implicit to Iterable[Char]?
On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison <alan.burlison@gmail.com> wrote:On 29/11/2011 21:15, Josh Suereth wrote:
file.getName.dropWhile(_ != '.') match {
case ".exe" => ...
case _ => ???
}
str.substring(str.lastIndexOf(".") + 1)
Wed, 2011-11-30, 06:27
#5
Re: Match file extension in file name
Elucidation ... ?
On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
Good exercise to learn to use zippers.
On Nov 30, 2011 2:26 PM, "Josh Suereth" <joshua.suereth@gmail.com> wrote:
drop actually handles that, see my second post :)
file.getName.drop(file.getName lastIndexOf '.') match { ...}
On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Neither of you handled lastIndexOf(".") == -1.Also will lastIndexOf('.') be handled by java.lang.String#lastIndexOf(Int) or by the implicit to Iterable[Char]?
On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison <alan.burlison@gmail.com> wrote:On 29/11/2011 21:15, Josh Suereth wrote:
file.getName.dropWhile(_ != '.') match {
case ".exe" => ...
case _ => ???
}
str.substring(str.lastIndexOf(".") + 1)
Wed, 2011-11-30, 07:27
#6
Re: Match file extension in file name
Not sure what more explanation will help. Use a list zipper to solve the
problem. This problem is trivial enough to introduce the concept and
appropriate enough to demonstrate the purpose of a list zipper. Assume
that a filename is a List[Char].
On 30/11/11 16:19, Naftoli Gugenheim wrote:
> Elucidation ... ?
>
>
> On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris wrote:
>
>> Good exercise to learn to use zippers.
>> On Nov 30, 2011 2:26 PM, "Josh Suereth" wrote:
>>
>>> drop actually handles that, see my second post :)
>>>
>>> file.getName.drop(file.getName lastIndexOf '.') match {
>>> ...
>>> }
>>>
>>> On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim >>> wrote:
>>>> Neither of you handled lastIndexOf(".") == -1.
>>>> Also will lastIndexOf('.') be handled by
>>>> java.lang.String#lastIndexOf(Int) or by the implicit to Iterable[Char]?
>>>>
>>>>
>>>> On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison wrote:
>>>>
>>>>> On 29/11/2011 21:15, Josh Suereth wrote:
>>>>>
>>>>> file.getName.dropWhile(_ != '.') match {
>>>>>> case ".exe" => ...
>>>>>> case _ => ???
>>>>>> }
>>>>>>
>>>>> str.substring(str.lastIndexOf(**".") + 1)
>>>>>
>>>>> --
>>>>> Alan Burlison
>>>>> --
>>>>>
>>>>
Wed, 2011-11-30, 07:37
#7
Re: Match file extension in file name
Most people in the world who wanted to introduce people to a concept (such as zippers in this case) at such a point in a conversation, would do so with an illustration, not by advertising the existence of a good exercise.
On Wed, Nov 30, 2011 at 1:24 AM, Tony Morris <tonymorris@gmail.com> wrote:
On Wed, Nov 30, 2011 at 1:24 AM, Tony Morris <tonymorris@gmail.com> wrote:
Not sure what more explanation will help. Use a list zipper to solve the
problem. This problem is trivial enough to introduce the concept and
appropriate enough to demonstrate the purpose of a list zipper. Assume
that a filename is a List[Char].
On 30/11/11 16:19, Naftoli Gugenheim wrote:
> Elucidation ... ?
>
>
> On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
>
>> Good exercise to learn to use zippers.
>> On Nov 30, 2011 2:26 PM, "Josh Suereth" <joshua.suereth@gmail.com> wrote:
>>
>>> drop actually handles that, see my second post :)
>>>
>>> file.getName.drop(file.getName lastIndexOf '.') match {
>>> ...
>>> }
>>>
>>> On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com
>>>> wrote:
>>>> Neither of you handled lastIndexOf(".") == -1.
>>>> Also will lastIndexOf('.') be handled by
>>>> java.lang.String#lastIndexOf(Int) or by the implicit to Iterable[Char]?
>>>>
>>>>
>>>> On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison <alan.burlison@gmail.com>wrote:
>>>>
>>>>> On 29/11/2011 21:15, Josh Suereth wrote:
>>>>>
>>>>> file.getName.dropWhile(_ != '.') match {
>>>>>> case ".exe" => ...
>>>>>> case _ => ???
>>>>>> }
>>>>>>
>>>>> str.substring(str.lastIndexOf(**".") + 1)
>>>>>
>>>>> --
>>>>> Alan Burlison
>>>>> --
>>>>>
>>>>
--
Tony Morris
http://tmorris.net/
Wed, 2011-11-30, 08:07
#8
Re: Match file extension in file name
Cool story bro. Let me know how you get on.
On 30/11/11 17:34, Naftoli Gugenheim wrote:
> Most people in the world who wanted to introduce people to a concept (such
> as zippers in this case) at such a point in a conversation, would do so
> with an illustration, not by advertising the existence of a good exercise.
>
>
> On Wed, Nov 30, 2011 at 1:24 AM, Tony Morris wrote:
>
>> Not sure what more explanation will help. Use a list zipper to solve the
>> problem. This problem is trivial enough to introduce the concept and
>> appropriate enough to demonstrate the purpose of a list zipper. Assume
>> that a filename is a List[Char].
>>
>>
>> On 30/11/11 16:19, Naftoli Gugenheim wrote:
>>> Elucidation ... ?
>>>
>>>
>>> On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris
>> wrote:
>>>> Good exercise to learn to use zippers.
>>>> On Nov 30, 2011 2:26 PM, "Josh Suereth"
>> wrote:
>>>>> drop actually handles that, see my second post :)
>>>>>
>>>>> file.getName.drop(file.getName lastIndexOf '.') match {
>>>>> ...
>>>>> }
>>>>>
>>>>> On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <
>> naftoligug@gmail.com
>>>>>> wrote:
>>>>>> Neither of you handled lastIndexOf(".") == -1.
>>>>>> Also will lastIndexOf('.') be handled by
>>>>>> java.lang.String#lastIndexOf(Int) or by the implicit to
>> Iterable[Char]?
>>>>>>
>>>>>> On Tue, Nov 29, 2011 at 7:45 PM, Alan Burlison <
>> alan.burlison@gmail.com>wrote:
>>>>>>> On 29/11/2011 21:15, Josh Suereth wrote:
>>>>>>>
>>>>>>> file.getName.dropWhile(_ != '.') match {
>>>>>>>> case ".exe" => ...
>>>>>>>> case _ => ???
>>>>>>>> }
>>>>>>>>
>>>>>>> str.substring(str.lastIndexOf(**".") + 1)
>>>>>>>
>>>>>>> --
>>>>>>> Alan Burlison
>>>>>>> --
>>>>>>>
>>
>> --
>> Tony Morris
>> http://tmorris.net/
>>
>>
>>
Wed, 2011-11-30, 10:37
#9
Re: Match file extension in file name
On Wed, Nov 30, 2011 at 6:19 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Zippers are a cursor that can be moved around a list. At all times, you are focussed on one element, and can look left and right. You can also modify the structure -- inserting or deleting elements. Much like lenses, they provide an illusion of imperative updates of immutable data structures.
Here's a small example:
https://github.com/scalaz/scalaz/commit/2917d6f8e553ab48dde4a4d26e21ac0dab64b3fb
If you perform more complex search/replace operations (e.g. find the second place where the predicate holds and delete the two surrounding elements), they provide a clean programming model. For something as simple as matching the extension of a filename, I wouldn't bother.
scalaz.Zipper views a sequence of elements, but the idea is more general. We also have scalaz.TreeLoc (a zipper over a tree). There have even been experiments to automatically create zippers over case classes with a compiler plugin [1]
-jason
[1] http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-structures
Elucidation ... ?
On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
Good exercise to learn to use zippers.
Zippers are a cursor that can be moved around a list. At all times, you are focussed on one element, and can look left and right. You can also modify the structure -- inserting or deleting elements. Much like lenses, they provide an illusion of imperative updates of immutable data structures.
Here's a small example:
https://github.com/scalaz/scalaz/commit/2917d6f8e553ab48dde4a4d26e21ac0dab64b3fb
If you perform more complex search/replace operations (e.g. find the second place where the predicate holds and delete the two surrounding elements), they provide a clean programming model. For something as simple as matching the extension of a filename, I wouldn't bother.
scalaz.Zipper views a sequence of elements, but the idea is more general. We also have scalaz.TreeLoc (a zipper over a tree). There have even been experiments to automatically create zippers over case classes with a compiler plugin [1]
-jason
[1] http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-structures
Wed, 2011-11-30, 16:27
#10
Re: Match file extension in file name
+1 on that one...
On Wed, Nov 30, 2011 at 10:19 AM, Rex Kerr <ichoran@gmail.com> wrote:
On Wed, Nov 30, 2011 at 10:19 AM, Rex Kerr <ichoran@gmail.com> wrote:
On Tue, Nov 29, 2011 at 10:13 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Neither of you handled lastIndexOf(".") == -1.
This is why I prefer (unless performance is critical, which I have yet to ever find true for filename extensions)
file.getName.split('.').drop(1).lastOption.match {
case Some("exe") => ...
...
case None => ...
}
(or .tail.last instead of .drop(1).lastOption if I want an exception when there is no extension).
--Rex
Fri, 2011-12-02, 22:47
#11
Re: Match file extension in file name
I get an error saying copy is not a member of Zipper[Char]. I'm not sure what i'm doing wrong?
import scalaz.Lists
import scalaz.Zipper
import scalaz.Zippers
object ZipperDemo extends App with Lists with Zippers {
val fileName = "filename.ext....drg"
val ext = ListTo(fileName toList).toZipper match {
case Some(z) => z move (z.length - 1) match {
case Some(zEnd) => zEnd findPrevious (_ == '.') match {
case Some(zExt) => zExt.rights mkString
}
}
}
println(ext)
val lhs = fileName.substring(0, (fileName.length - 1) / 2).toStream
val h = fileName((fileName.length - 1) / 2 + 1)
val rhs = fileName.substring((fileName.length - 1) / 2 + 2).toStream
val zip = zipper(lhs, h, rhs)
for {
zEnd <- zip move (zip.length - 1)
zDot <- zEnd findPrevious (_ == '.')
zNew <- zDot.copy(rights = "zooppa".toStream) // getting error here!
} println(zDot.rights.mkString)
}
thx
On Wed, Nov 30, 2011 at 1:33 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
import scalaz.Lists
import scalaz.Zipper
import scalaz.Zippers
object ZipperDemo extends App with Lists with Zippers {
val fileName = "filename.ext....drg"
val ext = ListTo(fileName toList).toZipper match {
case Some(z) => z move (z.length - 1) match {
case Some(zEnd) => zEnd findPrevious (_ == '.') match {
case Some(zExt) => zExt.rights mkString
}
}
}
println(ext)
val lhs = fileName.substring(0, (fileName.length - 1) / 2).toStream
val h = fileName((fileName.length - 1) / 2 + 1)
val rhs = fileName.substring((fileName.length - 1) / 2 + 2).toStream
val zip = zipper(lhs, h, rhs)
for {
zEnd <- zip move (zip.length - 1)
zDot <- zEnd findPrevious (_ == '.')
zNew <- zDot.copy(rights = "zooppa".toStream) // getting error here!
} println(zDot.rights.mkString)
}
thx
On Wed, Nov 30, 2011 at 1:33 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
On Wed, Nov 30, 2011 at 6:19 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Elucidation ... ?
On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
Good exercise to learn to use zippers.
Zippers are a cursor that can be moved around a list. At all times, you are focussed on one element, and can look left and right. You can also modify the structure -- inserting or deleting elements. Much like lenses, they provide an illusion of imperative updates of immutable data structures.
Here's a small example:
https://github.com/scalaz/scalaz/commit/2917d6f8e553ab48dde4a4d26e21ac0dab64b3fb
If you perform more complex search/replace operations (e.g. find the second place where the predicate holds and delete the two surrounding elements), they provide a clean programming model. For something as simple as matching the extension of a filename, I wouldn't bother.
scalaz.Zipper views a sequence of elements, but the idea is more general. We also have scalaz.TreeLoc (a zipper over a tree). There have even been experiments to automatically create zippers over case classes with a compiler plugin [1]
-jason
[1] http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-structures
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
Fri, 2011-12-02, 23:07
#12
Re: Match file extension in file name
On Fri, Dec 2, 2011 at 10:31 PM, mark dufresne <mark.dufresne@gmail.com> wrote:
Oh sorry, I should have mentioned that I was working with the scalaz-seven branch. I just added that `copy` for this example, as I couldn't find an existing method that conveniently allowed me to delete all the `lefts` or `rights`.
-jason
I get an error saying copy is not a member of Zipper[Char]. I'm not sure what i'm doing wrong?
Oh sorry, I should have mentioned that I was working with the scalaz-seven branch. I just added that `copy` for this example, as I couldn't find an existing method that conveniently allowed me to delete all the `lefts` or `rights`.
-jason
Sat, 2011-12-03, 00:57
#13
Re: Match file extension in file name
This looks pretty clearly like not the right level of abstraction to solve this problem. (Fine as an exercise to learn about zippers, though, I suppose.)
--Rex
On Fri, Dec 2, 2011 at 4:31 PM, mark dufresne <mark.dufresne@gmail.com> wrote:
--Rex
On Fri, Dec 2, 2011 at 4:31 PM, mark dufresne <mark.dufresne@gmail.com> wrote:
I get an error saying copy is not a member of Zipper[Char]. I'm not sure what i'm doing wrong?
import scalaz.Lists
import scalaz.Zipper
import scalaz.Zippers
object ZipperDemo extends App with Lists with Zippers {
val fileName = "filename.ext....drg"
val ext = ListTo(fileName toList).toZipper match {
case Some(z) => z move (z.length - 1) match {
case Some(zEnd) => zEnd findPrevious (_ == '.') match {
case Some(zExt) => zExt.rights mkString
}
}
}
println(ext)
val lhs = fileName.substring(0, (fileName.length - 1) / 2).toStream
val h = fileName((fileName.length - 1) / 2 + 1)
val rhs = fileName.substring((fileName.length - 1) / 2 + 2).toStream
val zip = zipper(lhs, h, rhs)
for {
zEnd <- zip move (zip.length - 1)
zDot <- zEnd findPrevious (_ == '.')
zNew <- zDot.copy(rights = "zooppa".toStream) // getting error here!
} println(zDot.rights.mkString)
}
thx
On Wed, Nov 30, 2011 at 1:33 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
On Wed, Nov 30, 2011 at 6:19 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Elucidation ... ?
On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris <tmorris@tmorris.net> wrote:
Good exercise to learn to use zippers.
Zippers are a cursor that can be moved around a list. At all times, you are focussed on one element, and can look left and right. You can also modify the structure -- inserting or deleting elements. Much like lenses, they provide an illusion of imperative updates of immutable data structures.
Here's a small example:
https://github.com/scalaz/scalaz/commit/2917d6f8e553ab48dde4a4d26e21ac0dab64b3fb
If you perform more complex search/replace operations (e.g. find the second place where the predicate holds and delete the two surrounding elements), they provide a clean programming model. For something as simple as matching the extension of a filename, I wouldn't bother.
scalaz.Zipper views a sequence of elements, but the idea is more general. We also have scalaz.TreeLoc (a zipper over a tree). There have even been experiments to automatically create zippers over case classes with a compiler plugin [1]
-jason
[1] http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-structures
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8
Sat, 2011-12-03, 10:17
#14
Re: Match file extension in file name
A zipper is exactly the right tool to solve this problem. The pseudocode
should be roughly:
z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
That's exactly what I would say to an alien if he landed on the planet
and he asked me what it meant to "drop the file extension."
"Well, from the start (left-most), first you move a cursor to the last
occurrence of the character '.'. Then move one position to the right,
the drop all values to the right of the cursor. If any any point none of
this makes sense (there is no last occurrence of '.' or you cannot move
right), then do not advance the cursor at all."
case class ListZipper[A](lefts: List[A], focus: A, rights: List[A])
Enjoy.
On 03/12/11 09:52, Rex Kerr wrote:
> This looks pretty clearly like not the right level of abstraction to solve
> this problem. (Fine as an exercise to learn about zippers, though, I
> suppose.)
> --Rex
>
> On Fri, Dec 2, 2011 at 4:31 PM, mark dufresne wrote:
>
>> I get an error saying copy is not a member of Zipper[Char]. I'm not sure
>> what i'm doing wrong?
>>
>> import scalaz.Lists
>> import scalaz.Zipper
>> import scalaz.Zippers
>>
>> object ZipperDemo extends App with Lists with Zippers {
>> val fileName = "filename.ext....drg"
>> val ext = ListTo(fileName toList).toZipper match {
>> case Some(z) => z move (z.length - 1) match {
>> case Some(zEnd) => zEnd findPrevious (_ == '.') match {
>> case Some(zExt) => zExt.rights mkString
>> }
>> }
>> }
>> println(ext)
>>
>> val lhs = fileName.substring(0, (fileName.length - 1) / 2).toStream
>> val h = fileName((fileName.length - 1) / 2 + 1)
>> val rhs = fileName.substring((fileName.length - 1) / 2 + 2).toStream
>> val zip = zipper(lhs, h, rhs)
>> for {
>> zEnd <- zip move (zip.length - 1)
>> zDot <- zEnd findPrevious (_ == '.')
>> zNew <- zDot.copy(rights = "zooppa".toStream) // getting error here!
>> } println(zDot.rights.mkString)
>>
>> }
>>
>> thx
>>
>>
>> On Wed, Nov 30, 2011 at 1:33 AM, Jason Zaugg wrote:
>>
>>> On Wed, Nov 30, 2011 at 6:19 AM, Naftoli Gugenheim wrote:
>>>
>>>> Elucidation ... ?
>>>>
>>>>
>>>> On Wed, Nov 30, 2011 at 12:11 AM, Tony Morris wrote:
>>>>
>>>>> Good exercise to learn to use zippers.
>>>>>
>>> Zippers are a cursor that can be moved around a list. At all times, you
>>> are focussed on one element, and can look left and right. You can also
>>> modify the structure -- inserting or deleting elements. Much like lenses,
>>> they provide an illusion of imperative updates of immutable data structures.
>>>
>>> Here's a small example:
>>>
>>>
>>> https://github.com/scalaz/scalaz/commit/2917d6f8e553ab48dde4a4d26e21ac0d...
>>>
>>> If you perform more complex search/replace operations (e.g. find the
>>> second place where the predicate holds and delete the two surrounding
>>> elements), they provide a clean programming model. For something as simple
>>> as matching the extension of a filename, I wouldn't bother.
>>>
>>> scalaz.Zipper views a sequence of elements, but the idea is more general.
>>> We also have scalaz.TreeLoc (a zipper over a tree). There have even been
>>> experiments to automatically create zippers over case classes with a
>>> compiler plugin [1]
>>>
>>> -jason
>>>
>>> [1]
>>> http://stackoverflow.com/questions/3900307/cleaner-way-to-update-nested-...
>>>
>>>
>>
>>
>> --
>> "...I have suffered the loss of all things, and count them as rubbish,
>> that I may gain Christ" -Phil 3:8
>>
Sat, 2011-12-03, 13:37
#15
Re: Match file extension in file name
I sort of think a list zipper might be an expensive way to zip over a string (in some cases), since you have to convert the string first, then perform your algorithm.
If strings *were* lists in scala it would be a no-brainer. Does Scalaz have a String optimized zipper or are they locked to lists?
Sat, 2011-12-03, 16:37
#16
Re: Match file extension in file name
On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris <tonymorris@gmail.com> wrote:
A zipper is exactly the right tool to solve this problem. The pseudocode
should be roughly:
z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
That's not even the same problem--the problem was to match on the extension.
(Also, I don't think you want to moveRight--normally people expect the dot to disappear with the rest of the extension so that you don't have to special-case extensionless filenames when stripping and adding extensions back on again.)
I agree that if this _was_ the problem, this would be an effective solution (if you have this library and not others that would allow you to work at the token level instead of the character level).
--Rex
Sun, 2011-12-04, 14:07
#17
Re: Match file extension in file name
If you have all the implicits in place, maybe. Otherwise it's hard to beat
s.split('.').drop(1).lastOption
both for compactness and conceptual simplicity. Especially if you have to deal with multiple extensions (e.g. .tar.gz) and you thus do something like
s.split('.').drop(1).reverse.takeWhile(isKnownExtension).reverse
(Using a zipper on the split tokens would also be good if all the relevant implicits exist.)
--Rex
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
s.split('.').drop(1).lastOption
both for compactness and conceptual simplicity. Especially if you have to deal with multiple extensions (e.g. .tar.gz) and you thus do something like
s.split('.').drop(1).reverse.takeWhile(isKnownExtension).reverse
(Using a zipper on the split tokens would also be good if all the relevant implicits exist.)
--Rex
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oh I thought I saw some code along the way (in the thread) that dropped
the file extension. Still, the problem is best solved with the list zipper.
On 12/04/2011 01:28 AM, Rex Kerr wrote:
> On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris <tonymorris@gmail.com> wrote:
>
>> A zipper is exactly the right tool to solve this problem. The pseudocode
>> should be roughly:
>>
>> z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
>>
>
> That's not even the same problem--the problem was to match on the extension.
>
> (Also, I don't think you want to moveRight--normally people expect the dot
> to disappear with the rest of the extension so that you don't have to
> special-case extensionless filenames when stripping and adding extensions
> back on again.)
>
> I agree that if this _was_ the problem, this would be an effective solution
> (if you have this library and not others that would allow you to work at
> the token level instead of the character level).
>
> --Rex
>
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJO22EDAAoJEPxHMY3rBz0PqiYH/1LHlqkzE5440tUps3hGYwwI
WrSfSVWWKDJmWz6DqK9jSWH58fvqqtb0qAaRcxNtRJZnrZBA/mK8zahNSb5kQJY3
zn5jCrimTvqn4drN9NxTuGmrHzCHuF5uC1G5HAdXzXGmchYnVKgrX7oTPOa8eGCm
Gh/H3YALL/8t0FHU5lo7cADOfhnf/U6aZp7A4yPYwlg4jPN64RF/QO12+syzMY2e
Yh/9k+BQ/E99wCpDrJ/eBza2lVeqC1gWUpovVEZQvkzkx2ZcWlitgHPDp6hXH7Q9
7UZ73QkRibdkrUQZftXL0MN5uVZoqpUU+pi/YZmhLHlJMt53N4vx8ESz645Rq4U=
=9XqC
-----END PGP SIGNATURE-----
Sun, 2011-12-04, 15:37
#18
Re: Match file extension in file name
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oh I thought I saw some code along the way (in the thread) that dropped
the file extension. Still, the problem is best solved with the list zipper.
On 12/04/2011 01:28 AM, Rex Kerr wrote:
> On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris wrote:
>
>> A zipper is exactly the right tool to solve this problem. The pseudocode
>> should be roughly:
>>
>> z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
>>
>
> That's not even the same problem--the problem was to match on the extension.
>
> (Also, I don't think you want to moveRight--normally people expect the dot
> to disappear with the rest of the extension so that you don't have to
> special-case extensionless filenames when stripping and adding extensions
> back on again.)
>
> I agree that if this _was_ the problem, this would be an effective solution
> (if you have this library and not others that would allow you to work at
> the token level instead of the character level).
>
> --Rex
>
Sun, 2011-12-04, 15:47
#19
Re: Match file extension in file name
I disagree with the "list" part of that statement. If Strings were represented as lists, then it'd be a no brainer perhaps.
Conceptually, using a zipper is the best approach, but I agree with Jason. For this language/runtime a list zipper is probably overkill for the problem.
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
Conceptually, using a zipper is the best approach, but I agree with Jason. For this language/runtime a list zipper is probably overkill for the problem.
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oh I thought I saw some code along the way (in the thread) that dropped
the file extension. Still, the problem is best solved with the list zipper.
On 12/04/2011 01:28 AM, Rex Kerr wrote:
> On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris <tonymorris@gmail.com> wrote:
>
>> A zipper is exactly the right tool to solve this problem. The pseudocode
>> should be roughly:
>>
>> z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
>>
>
> That's not even the same problem--the problem was to match on the extension.
>
> (Also, I don't think you want to moveRight--normally people expect the dot
> to disappear with the rest of the extension so that you don't have to
> special-case extensionless filenames when stripping and adding extensions
> back on again.)
>
> I agree that if this _was_ the problem, this would be an effective solution
> (if you have this library and not others that would allow you to work at
> the token level instead of the character level).
>
> --Rex
>
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJO22EDAAoJEPxHMY3rBz0PqiYH/1LHlqkzE5440tUps3hGYwwI
WrSfSVWWKDJmWz6DqK9jSWH58fvqqtb0qAaRcxNtRJZnrZBA/mK8zahNSb5kQJY3
zn5jCrimTvqn4drN9NxTuGmrHzCHuF5uC1G5HAdXzXGmchYnVKgrX7oTPOa8eGCm
Gh/H3YALL/8t0FHU5lo7cADOfhnf/U6aZp7A4yPYwlg4jPN64RF/QO12+syzMY2e
Yh/9k+BQ/E99wCpDrJ/eBza2lVeqC1gWUpovVEZQvkzkx2ZcWlitgHPDp6hXH7Q9
7UZ73QkRibdkrUQZftXL0MN5uVZoqpUU+pi/YZmhLHlJMt53N4vx8ESz645Rq4U=
=9XqC
-----END PGP SIGNATURE-----
Sun, 2011-12-04, 15:47
#20
Re: Match file extension in file name
I don't think we've heard Jason's view on the matter...maybe you were agreeing with me (again, since you seemed favorably disposed towards the solution the first time I presented it)?
--Rex
On Sun, Dec 4, 2011 at 9:36 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
--Rex
On Sun, Dec 4, 2011 at 9:36 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
I disagree with the "list" part of that statement. If Strings were represented as lists, then it'd be a no brainer perhaps.
Conceptually, using a zipper is the best approach, but I agree with Jason. For this language/runtime a list zipper is probably overkill for the problem.
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oh I thought I saw some code along the way (in the thread) that dropped
the file extension. Still, the problem is best solved with the list zipper.
On 12/04/2011 01:28 AM, Rex Kerr wrote:
> On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris <tonymorris@gmail.com> wrote:
>
>> A zipper is exactly the right tool to solve this problem. The pseudocode
>> should be roughly:
>>
>> z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
>>
>
> That's not even the same problem--the problem was to match on the extension.
>
> (Also, I don't think you want to moveRight--normally people expect the dot
> to disappear with the rest of the extension so that you don't have to
> special-case extensionless filenames when stripping and adding extensions
> back on again.)
>
> I agree that if this _was_ the problem, this would be an effective solution
> (if you have this library and not others that would allow you to work at
> the token level instead of the character level).
>
> --Rex
>
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJO22EDAAoJEPxHMY3rBz0PqiYH/1LHlqkzE5440tUps3hGYwwI
WrSfSVWWKDJmWz6DqK9jSWH58fvqqtb0qAaRcxNtRJZnrZBA/mK8zahNSb5kQJY3
zn5jCrimTvqn4drN9NxTuGmrHzCHuF5uC1G5HAdXzXGmchYnVKgrX7oTPOa8eGCm
Gh/H3YALL/8t0FHU5lo7cADOfhnf/U6aZp7A4yPYwlg4jPN64RF/QO12+syzMY2e
Yh/9k+BQ/E99wCpDrJ/eBza2lVeqC1gWUpovVEZQvkzkx2ZcWlitgHPDp6hXH7Q9
7UZ73QkRibdkrUQZftXL0MN5uVZoqpUU+pi/YZmhLHlJMt53N4vx8ESz645Rq4U=
=9XqC
-----END PGP SIGNATURE-----
Sun, 2011-12-04, 16:07
#21
Re: Match file extension in file name
Jaosn Zaugg - "scalaz.Zipper contains (Stream[A], A, Stream[A]). Once this is created, it's cheap to perform zip around and perform insertions / deletions, before finally converting the back to a List or String.
It's overkill for the originally stated problem, IMO, something like this would suffice.
scala> def prefix(suffix: String, text: String) = if (text.endsWith(suffix)) Some(text.stripSuffix(suffix)) else Noneprefix: (suffix: String, text: String)Option[String]
scala> prefix(".ext", "filename.ext")res6: Option[String] = Some(filename)
scala> prefix(".ext", "filename.txt")res7: Option[String] = None "
On Sun, Dec 4, 2011 at 9:46 AM, Rex Kerr <ichoran@gmail.com> wrote:
It's overkill for the originally stated problem, IMO, something like this would suffice.
scala> def prefix(suffix: String, text: String) = if (text.endsWith(suffix)) Some(text.stripSuffix(suffix)) else Noneprefix: (suffix: String, text: String)Option[String]
scala> prefix(".ext", "filename.ext")res6: Option[String] = Some(filename)
scala> prefix(".ext", "filename.txt")res7: Option[String] = None "
On Sun, Dec 4, 2011 at 9:46 AM, Rex Kerr <ichoran@gmail.com> wrote:
I don't think we've heard Jason's view on the matter...maybe you were agreeing with me (again, since you seemed favorably disposed towards the solution the first time I presented it)?
--Rex
On Sun, Dec 4, 2011 at 9:36 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:I disagree with the "list" part of that statement. If Strings were represented as lists, then it'd be a no brainer perhaps.
Conceptually, using a zipper is the best approach, but I agree with Jason. For this language/runtime a list zipper is probably overkill for the problem.
On Sun, Dec 4, 2011 at 7:01 AM, Tony Morris <tonymorris@gmail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oh I thought I saw some code along the way (in the thread) that dropped
the file extension. Still, the problem is best solved with the list zipper.
On 12/04/2011 01:28 AM, Rex Kerr wrote:
> On Fri, Dec 2, 2011 at 7:01 PM, Tony Morris <tonymorris@gmail.com> wrote:
>
>> A zipper is exactly the right tool to solve this problem. The pseudocode
>> should be roughly:
>>
>> z.findLast(_ == '.') >=> moveRight >=> dropRights ||| z
>>
>
> That's not even the same problem--the problem was to match on the extension.
>
> (Also, I don't think you want to moveRight--normally people expect the dot
> to disappear with the rest of the extension so that you don't have to
> special-case extensionless filenames when stripping and adding extensions
> back on again.)
>
> I agree that if this _was_ the problem, this would be an effective solution
> (if you have this library and not others that would allow you to work at
> the token level instead of the character level).
>
> --Rex
>
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJO22EDAAoJEPxHMY3rBz0PqiYH/1LHlqkzE5440tUps3hGYwwI
WrSfSVWWKDJmWz6DqK9jSWH58fvqqtb0qAaRcxNtRJZnrZBA/mK8zahNSb5kQJY3
zn5jCrimTvqn4drN9NxTuGmrHzCHuF5uC1G5HAdXzXGmchYnVKgrX7oTPOa8eGCm
Gh/H3YALL/8t0FHU5lo7cADOfhnf/U6aZp7A4yPYwlg4jPN64RF/QO12+syzMY2e
Yh/9k+BQ/E99wCpDrJ/eBza2lVeqC1gWUpovVEZQvkzkx2ZcWlitgHPDp6hXH7Q9
7UZ73QkRibdkrUQZftXL0MN5uVZoqpUU+pi/YZmhLHlJMt53N4vx8ESz645Rq4U=
=9XqC
-----END PGP SIGNATURE-----
Sun, 2011-12-04, 16:07
#22
Re: Match file extension in file name
Weird, I missed that one. It's not even in my inbox. Thanks for the clarification!
--Rex
On Sun, Dec 4, 2011 at 9:49 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
--Rex
On Sun, Dec 4, 2011 at 9:49 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
Jaosn Zaugg - "scalaz.Zipper contains (Stream[A], A, Stream[A]). Once this is created, it's cheap to perform zip around and perform insertions / deletions, before finally converting the back to a List or String.
It's overkill for the originally stated problem, IMO, something like this would suffice.
scala> def prefix(suffix: String, text: String) = if (text.endsWith(suffix)) Some(text.stripSuffix(suffix)) else Noneprefix: (suffix: String, text: String)Option[String]
scala> prefix(".ext", "filename.ext")res6: Option[String] = Some(filename)
scala> prefix(".ext", "filename.txt")res7: Option[String] = None "
thanks guys
On Tue, Nov 29, 2011 at 10:53 AM, Michael Schmitz <michael@schmitztech.com> wrote:
--
"...I have suffered the loss of all things, and count them as rubbish, that I may gain Christ" -Phil 3:8