2 people like it.

Pex4fun - Merging strings

A random pex4fun puzzle to write a snippet to merge two strings.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
module Program

  let puzzle (s: string, t: string) : string =
    let sArr = Array.ofSeq s |> Array.map(fun x -> x.ToString())
    let tArr = Array.ofSeq t |> Array.map(fun y -> y.ToString())
    let result arr1 arr2 =
      let len1 = arr1 |> Array.length
      let len2 = arr2 |> Array.length
      let less = min len1 len2
      let count = abs (len1 - len2)
      let fold = Array.fold(fun s e -> s+e) ""
      let first = [| for i=0 to less-1 do yield sArr.[i] + tArr.[i]|] |> fold
      let rest  = Array.init count (fun i -> if less = len1 then arr2.[i+less] else arr1.[i+less]) |> fold
      first + rest
    result sArr tArr

  // "eafbgchdijk"
  puzzle ("efghijk", "abcd")

  // "aebfcgdhijk"
  puzzle ("abcd", "efghijk")
module Program
val puzzle : s:string * t:string -> string

Full name: Program.puzzle
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val t : string
val sArr : string []
module Array

from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.ofSeq
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val x : char
System.Char.ToString() : string
System.Char.ToString(provider: System.IFormatProvider) : string
val tArr : string []
val y : char
val result : (string [] -> string [] -> string)
val arr1 : string []
val arr2 : string []
val len1 : int
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
val len2 : int
val less : int
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val count : int
val abs : value:'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
val fold : (string [] -> string)
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold
val e : string
val first : string
val i : int
val rest : string
val init : count:int -> initializer:(int -> 'T) -> 'T []

Full name: Microsoft.FSharp.Collections.Array.init

More information

Link:http://fssnip.net/eP
Posted:4 years ago
Author:Joel Huang
Tags: string , pex4fun , fold , generic functions