F#: Reducing an array of numbers as strings to a single integer

Stefan Perko

I've got an array of strings, call it a, where each individual string represents a number. I also have a function f : int -> int -> int, which I want to use to "reduce a to a single number". I would like to write:

a |> Array.reduce (fun x y -> f (int32 x) (int32 y))

but this does not work, because the type of "reduce" forbids me from returning integers from f (since a is an array of strings)

Is there a functional way to make this work without having to return a string from f or casting the string array to an int array beforehand?

Carsten

using Array.reduce mapping first

If you don't want to adapt your f to handle strings, and you want to use Array.reduce then yeah I guess you should convert first (and to be honest: it seems easier than doing it manually with your wrapper-lambda) - so why not just use

a
|> Array.map int32
|> Array.reduce f

instead?

if you are concerned with the overhead of producing an intermediate Array you can always switch Array. with Seq. to to it lazily:

a |> Seq.map int32 |> Seq.reduce f

using Array.fold

aside from that you can always fold to your hearts desire:

a |> Array.fold (fun n s -> n + int32 s) 0

so you might call this more functional or not ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

F#: Reducing an array of numbers as strings to a single integer

From Dev

Converting 'integer strings' to integer array

From Dev

Finding a single integer in an array

From Dev

numbers and strings in array python

From Dev

convert array of numbers that are strings into array of numbers

From Dev

Split Numbers String to Integer Array

From Dev

Go: convert strings in array to integer

From Dev

Convert array of strings to one integer

From Dev

Regex to replace decimal and integer strings with numbers

From Dev

Regex to replace decimal and integer strings with numbers

From Dev

Convert an array of Integer into a single String

From Dev

Python: Reducing a set of strings

From Dev

pushing a var of numbers that are strings into an array

From Dev

Range of numbers to array of number strings?

From Dev

Remove numbers from strings in array

From Dev

Separating numbers to different strings or an array

From Dev

Range of numbers to array of number strings?

From Dev

list with numbers and strings to single string python

From Dev

reducing an array into another array

From Dev

String with Comma separated numbers to array of integer in javascript

From Dev

how to convert integer array to separated numbers in php

From Dev

Parse string containing numbers into integer array

From Dev

Extract float numbers from integer array

From Dev

Generate Integer Random Numbers in Python Array

From Dev

C - String of numbers separated by whitespaces into integer array

From Dev

sort array with integer strings type in jQuery

From Dev

Compare two strings and store the index in integer array

From Dev

Converting an array of integers into a single integer using jquery

From Dev

Convert int array into single integer in C

Related Related

  1. 1

    F#: Reducing an array of numbers as strings to a single integer

  2. 2

    Converting 'integer strings' to integer array

  3. 3

    Finding a single integer in an array

  4. 4

    numbers and strings in array python

  5. 5

    convert array of numbers that are strings into array of numbers

  6. 6

    Split Numbers String to Integer Array

  7. 7

    Go: convert strings in array to integer

  8. 8

    Convert array of strings to one integer

  9. 9

    Regex to replace decimal and integer strings with numbers

  10. 10

    Regex to replace decimal and integer strings with numbers

  11. 11

    Convert an array of Integer into a single String

  12. 12

    Python: Reducing a set of strings

  13. 13

    pushing a var of numbers that are strings into an array

  14. 14

    Range of numbers to array of number strings?

  15. 15

    Remove numbers from strings in array

  16. 16

    Separating numbers to different strings or an array

  17. 17

    Range of numbers to array of number strings?

  18. 18

    list with numbers and strings to single string python

  19. 19

    reducing an array into another array

  20. 20

    String with Comma separated numbers to array of integer in javascript

  21. 21

    how to convert integer array to separated numbers in php

  22. 22

    Parse string containing numbers into integer array

  23. 23

    Extract float numbers from integer array

  24. 24

    Generate Integer Random Numbers in Python Array

  25. 25

    C - String of numbers separated by whitespaces into integer array

  26. 26

    sort array with integer strings type in jQuery

  27. 27

    Compare two strings and store the index in integer array

  28. 28

    Converting an array of integers into a single integer using jquery

  29. 29

    Convert int array into single integer in C

HotTag

Archive