DEV Community

loading...

Discussion on: Daily Challenge #1 - String Peeler

Collapse
peterbunting profile image
Peter Bunting

In F#. Not the most straightforward way to solve this, but I wanted to use the language Array splicing.

let stringToByteArray (s:string)= System.Text.Encoding.ASCII.GetBytes s
let byteArraySplice x y (b: byte[]) = b.[x..y]
let byteArrayToString (b: byte[]) = System.Text.Encoding.ASCII.GetString b

let stringPeeler s = 
    s
    |> stringToByteArray
    |> byteArraySplice 1 (s.Length - 2)
    |> byteArrayToString

printfn "%s" <| stringPeeler "abcdefgh"
printfn "%s" <| stringPeeler "ab"
printfn "%s" <| stringPeeler "a"
Collapse
peterbunting profile image
Peter Bunting

And it turns out that you don't even need to explicitly convert it to an array. F# will let you do splicing on a string. So a much better solution is:

let stringPeeler (s: string) = s.[1..(s.Length - 2)]