f#,mapping,intervals,discrete-mathematics , Combine data into smaller discrete intervals

## Question:

Tag: f#,mapping,intervals,discrete-mathematics

Suppose we have a pair of input arrays, or a list of (key, value) tuples if you prefer. What's an elegant and performant way to combine values that have indices falling in a certain interval? For example, if the interval (or 'bin') size is 10 then the values of all indices from 0 < x <= 10 would be combined, as would the values of indices from 10 < x <= 20 and so on. I want:

let interval = 10
let index  = [| 6; 12; 18; 24 |]
let value  = [| a;  b;  c;  d |]
result = [| a; b + c; d |]

The crudest way to do this would be to use a whole lot of if, else if statements (the index range has a defined upper limit). I got close with

for i = 0 to index.Length do
result.[Math.Floor(index.[i]/10] += value.[Math.Floor(index.[i]/10]

but this is doing 0 <= x < 10, not 0 < x <= 10.

I also tried assuming the indices are ordered and evenly spaced, with

for i = 1 : ( index.Length - 1 ) / valuesPerBin
valueRange = ((i-1)*valuesPerBin + 1) : i*valuesPerBin )
result(i) = sum(value(valueRange))

which is nice but obviously breaks if there is a non integer number of values per bin.

What's the best way of doing this in F#? Is there a name or an existing function for what I'm trying to do?

let interval = 10
let index = [6;12;18;24]
let value =[101;102;103;104]
let intervals = List.map (fun e -> e/interval) index
let keys = List.map2(fun e1 e2 -> (e1,e2)) intervals value
let skeys = Seq.ofList keys
let result = skeys
|>Seq.groupBy (fun p -> fst p)
|>Seq.map (fun p -> snd p)
|>Seq.map(fun s -> Seq.sumBy (fun p -> snd p) s)

result will be [101;205;104] (as a Seq).

If you want to convert to an array, apply Seq.toArray.

Is it what you wanted ?

# Related:

## How to handle Arithmetic operation OverflowException in F#?

python,f#
I'm doing Project Euler problem 1 in F#: Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples...

## Referencing event from .xaml file in .fs file

wpf,xaml,f#,fsxaml
I want to declare an event in a XAML file and then add a handler in a fs file. The upper part of the XAML file would be something like (MouseRightButtonDown): <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="AboutStack" MouseRightButtonDown="AboutStack_MouseRightButtonDown" Title="About" SizeToContent="WidthAndHeight"> <StackPanel>... The F# file contains: open FsXaml type MainWindow = XAML<"MainWindow.xaml", true>...

## JavaScript objects: 'addEventListener is not a function'

javascript,html,object,canvas,mapping
I create an array of JavaScript objects called elements and loop through it in order to draw every one on a canvas as well as hooking up a few event handlers to it. However, I get an error stating that 'Uncaught TypeError: elements[i].addEventListener is not a function' Here is the...

## Conditional Operator in F# (A?B:C) [duplicate]

c#,f#,conditional-operator
This question already has an answer here: Does F# have the ternary ?: operator? 3 answers In C#, we have the conditional operator: [condition] ? [value if true] : [value if false] But I can't seem to find this in F#. Does it exist?...

## Change lowercase and uppercase of characters in java

java,unicode,mapping,uppercase,lowercase
If I want to create a dictionary where the user can create a custom alphabet (that still uses unicode) Is there a way to change lowercase and uppercase mapping of the characters? Let's say I want the lowercase of 'I' to be 'ı' instead of 'i' or upperCase of 'b'...

## How to rewrite this C# code

f#
I have this C# code: const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = stream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } I need to rewrite it in F#. I can do it like this: let bufferLen : int =...

## Filtering a sequence of options and collecting the values of all the Somes. Is there a built-in function in F# for that?

.net,f#
A common pattern in F# is to want to filter something by whether it is "Some"thing and in case it is, getting its value: module Option = let values s = s |> Seq.filter Option.isSome |> Seq.map Option.get I have the idea I've seen something like that in the F#...

## R Creating a Character Column from a Numeric Column w/o using For Loop

r,for-loop,mapping,character,lookup
I am trying to create a column of characters based on an existing column of numbers, preferably without using a for loop. I have come up with a variety of ways to do this, but I keep feeling like I'm making this far more complicated than it needs to be....

## How to recursively identify cells of specific type in grid?

recursion,f#,tail-recursion
I'm learning F# and I'm building a minesweeper app. As part of that, I'm trying to have a method that detonates all adjacent mines if a mine is detonated, recursively. So if I have a grid like: | 0 | 1 | 2 | ------------------------ 0 | E-1 | M...

## how to call function from library in formula with R type provider

r,f#
I am following some R examples from a book on stochastic simulation, and being familiar with F# and not familiar with R -I decided to try out the R type provider. Today I come across a code snippet which I cannot figure out how to execute through the R type...

## When are let bindings in code quotations replaced with their values?

f#
If I run the first example from MSDN (https://msdn.microsoft.com/en-us/library/dd233212.aspx) in the F# Interactive window, I get the expected output: fun (x:System.Int32) -> x + 1 a + 1 let f = fun (x:System.Int32) -> x + 10 in f 10 But if I run it in the Main from my...

## Add brands through company, it's possible? How?

php,symfony2,doctrine2,mapping,symfony-2.6
I have this two tables (see pics below) mapped as follow: class Brand { ... /** * @var Company * * @ORM\ManyToOne(targetEntity="Company") * @ORM\JoinColumn(name="companies_id", referencedColumnName="id") */ protected \$company; } class Company { ... } I need to add support for add a new Brand from Company but I have not...

## Control pan and zoom animation duration in mapbox.js

mapping,gis,mapbox,mapbox-gl-js
I'm making an animated map showing a series of points using Mapbox.js. Ideally, I want to smoothly switch focus between points by combining zoom and pan like this example created in d3.js. I wonder if there is anyway to control the pan & zoom animation speed (mainly to slow it...

## F# Discriminated Union - “downcasting” to subtype

f#,discriminated-union
I don't really know what the proper title for this question should be, but: I have a discriminated union called MyDiscriminatedUnion in F#: type MyDiscriminatedUnion = | Foo of Foo | Bar of Bar where Foo and Bar are record types: type Foo = { ... } type Bar =...

## Is this definition of a tail recursive fibonacci function tail-recursive?

scala,f#,functional-programming,tail-recursion,continuation-passing
I've seen around the following F# definition of a continuation-passing-style fibonacci function, that I always assumed to be tail recursive: let fib k = let rec fib' k cont = match k with | 0 | 1 -> cont 1 | k -> fib' (k-1) (fun a -> fib' (k-2)...

## F# - Recursive function to copy a list

list,f#
I wrote this code to recursively copy my list: let sp = [2;4;6;8;10] let copy (s1:'a list) = let rec copy acc ind = if(ind>=0) then copy (s1.[ind]::acc) (ind-1) else acc copy [] (s1.Length-1) sp |> copy |> printfn "%A" How to make this code easier?...

## Selenium Web Driver : How to map html elements to Java Object.

selenium,webdriver,mapping
As part of Selenium Web-driver learning I came across a scenario. Please let me know the professional approach to proceed. I am testing a eCommerce application where while I click on Mobile link all mobile phones are getting displayed.I want to check whether they are sorted based on name and...

## Mapping from One Sheet to Another Fill Unfilled chracter

So I have one excel document that has partially filled character array ("|" shows excel column line): Current Result ("_" is a space): 1111GGH80100022190 1112QQH80100023201 1113GGH80100045201 1114AAH80100025190 So my current code outputs this above result. The problem is that characters 1-5 and 21-24 get skipped over. In general if there...

## How convert any record into a map/dictionary in F#?

dictionary,f#,converter,record
I need to serialize arbitrary records into maps/dictionary. I imagine my end type look like this: type TabularData= array<Map<string, obj>> But I have problems in build a generic function that accept any record and turn them into Map....

## Properly implement F# Unit in C#

c#,f#,functional-programming
This question is not about C#/F# compatibility as in this one. I'd like to know the proper way to implement a type like F# Unit in place of using void. Obviously I'll discard this result and I'll not expose this type to the outside world. Wondering if an empty class...

## Using EF6 code-first, reference same property name

c#,entity-framework,properties,mapping
How to reference both ShippingAddressId and BillingAddressId properties in Customer class to Address class which has a diffrent key named AddressId? Running update-database -verbose causes error: Unable to determine the principal end of an association between the types 'Project1.Customer' and 'Project1.Address'. The principal end of this association must be explicitly...

## Does let!/do! always run the async object in a new thread?

f#
From the wikibook on F# there is a small section where it says: What does let! do?# let! runs an async<'a> object on its own thread, then it immediately releases the current thread back to the threadpool. When let! returns, execution of the workflow will continue on the new thread,...

## F# - writing an (GA) evaluation function in F#

f#
Background: I am currently working on a genetic algorithm (GA) in F#. (I come from a strong C# background and have only been using F# for a day now) Problem: I have an error evaluation function to determine how valid a proposed solution is.Lets assume a simplified error algorithm: Calculate...

## How can I resolve the “Could not fix timestamps in …” “…Error: The requested feature is not implemented.”

linux,build,f#
I have been trying to build a project in F# on Linux that I have located here on github. It's a basic kata project that I am working on as a demo. However on Linux (specifically Ubuntu 14.04 LTS Desktop) I haven't been able to get it to build yet...

## Texture mapping consuming physical memory

c++,opengl,mapping,textures,glut
there's a problem with my texture mapping, when I execute the code, it run smoothly for 5-8 sec, then the framerate drop drastically, when I monitor the task manager, it seem the program consuming almost 95% of my physical memory, anybody know how to solve this problem?? I'm using visual...

## Doc.Checkbox 'change' event does not occur in Websharper.UI.Next

web,f#,websharper
I have reactive Var variable varDone and Doc.Checkbox for its representation named cbDoc. After changing the value of varDone I need to call my function. For this, I wrote the code illustrated in the following pseudo-code: open WebSharper open WebSharper.UI.Next open WebSharper.UI.Next.Html open WebSharper.UI.Next.Notation // declaring reactive bool variable let...

## Return a modified version of same type in F#

f#
If I've got a class hierarchy like type Employee(name) = member val name: string = name type HourlyEmployee(name, rate) = inherit Employee(name) member val rate: int = rate type SalariedEmployee(name, salary) = inherit Employee(salary) member val salary: int = salary And I want a function that updates the name field...

## F# task parallelism under mono doesn't “appear” to execute in parallel

I have the following dummy code to test out TPL in F#. (Mono 4.5, xmarin studio, quard core macbook pri) To my surprise, all the processes are done on the same thread. there is not parallelism at all. open System open System.Threading open System.Threading.Tasks let doWork (num:int) (taskId:int) : unit...

## Duck typing with type provider instances [duplicate]

f#,type-providers,duck-typing
This question already has an answer here: How do I write this member constraint in F#? 1 answer I am writing an F# script to operating on tables in a database. All of the tables have a Guid Id property. I want to make use of duck typing to...

## F# “The value of constructor … not defined”

visual-studio-2010,f#
So, this is my second F# console application. The code goes like this: let rec fact n = if n = 1 then 1.0 else float(n)*fact(n-1);; let rec pow x n = if n = 1 then x else float(x) * pow x (n-1);; let rec func1 eps x n...

## Why is FSharp.Data.SqlClient record exposed as object?

f#,fsharp.data.sqlclient
I'm working on a demo where I can show how easy it is to use the SqlClient type provider in a F# library and then consume it from C#, I think this might be one way to get things into projects. Since many people using C# want to use interfaces...

## F# computation expression transparent state passing with Bind

f#,computation-expression
I have the following code that try to read a possibly incomplete data (image data, for example) from a network stream using usual MaybeBuilder: let image = maybe { let pos = 2 //Initial position skips 2 bytes of packet ID let! width, pos = readStreamAsInt 2 pos let! height,...

## interpreting a script through F#

scripting,f#,interpreted-language
I really like F# but I feel like it's not succint and short enough. I want to go further. I do have an idea of how I'd like to improve it but I have no experience in making compilers so I thought I'd make it a scripting language. Then I...

## Can you list the contents of a namespace or module in F#

f#
Similar to this question about clojure, is it possible to list the contents of a namespace or module in F#? When I open a namespace for a loaded DLL I'm not receiving an error that the namespace doesn't exist, but then when I try to use the documented functions I'm...

## All the F# templates in VS 2015 RC disappear

templates,f#,visual-studio-2015
I installed in a fresh Win 8.1 machine VS 2015 RC. I load VS and create a F# project. Fine. Eventually, I do some other stuff (that I have not remember) and then I found that no single F# template (for projects) exist anymore in VS. Only C#/VB ones....

## shift/reduce and reduce/reduce errors in F# using fsyacc and fslex

parsing,f#,dsl,lexer,fsyacc
I'm creating my own external DSL, which I intend to be pretty small and include some basic functionality. When I compile my project, it builds successfully but it shows a lot of shift/reduce and reduce/reduce errors. And most of them are around my binary operators. Here is part of my...

## Asynchronously manipulating data from streamReader in F#

On the line of Read large txt file multithreaded?, I have the doubt of whether it is equivalent to pass to each thread an sliced chunk of a Seq and whether it will safely handle the paralellism; is it StreamReader thread-safe? Here is the code I am using to test...

## F# Custom operators

I've defined a custom operator in F# like this: static member (&?) value defaultValue = if value = null then defaultValue else value The operator is defined within a type and should be called in the following scenario: I'm retrieving information about the system processors using WMI. Now i want...

## What is the difference between member val and member this in F#?

.net,f#,immutability
When I created a class containing a generic, mutable .NET Stack in F# like the example below, that stack ignores anything I push onto it. open System.Collections.Generic type Interp(code: int array) = member val PC = 0 with get, set member this.stack: Stack<int> = new Stack<int>() member this.Code = code...

## When I try to map the properties for an entity (Entity Framework), I get the error the type '__' must be a non-nullable value type

c#,entity-framework,generics,mapping,nullable
My mapping class looks like the following: public class EmployeeMap : EntityTypeConfiguration<EFEmployee> { public EmployeeMap() { HasKey(t => t.Id); Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Property(t => t.HireDate).IsRequired(); //Property(t => t.JobTitle).IsRequired(); Property(t => t.Salary).IsRequired(); //Property(t => t.Certifications).IsRequired(); Property(t => t.VacationTime).IsRequired(); Property(t => t.SickTime).IsRequired(); //Property(t =>...

## Redefine jQuery dependency search path

f#,resources,offline,websharper
I have found that a typical Websharper "hello world" application does not work offline due to the inaccessibility of jQuery resource "http://code.jquery.com/jquery-1.11.2.min.js". The question is: can I override this search path to the local folder of my web application?

## F# - Result is list of list of int instead of a list of int

f#
I have quite a complex problem that I have simplified for the purpose of this question. Let's just say the problem is as follow: I want a list of the first 3 numbers (1,2,3) added to all of these numbers: 0,10,20 So I want the following desired output: all =...

## Mapping int to int (in Java)

java,algorithm,dictionary,integer,mapping
In Java. How can I map a set of numbers(integers for example) to another set of numbers? All the numbers are positive and all the numbers are unique in their own set. The first set of numbers can have any value, the second set of numbers represent indexes of an...

## F# I want to filter my output

f#
I have a problem which I have simplified for the purpose of this question. Let's just say I have 2 lists. The first actually represents a list of classes but for this purpose, let's say it simply represents a list of ints (2,4,6,8,10). I have another list of ints that...

## Why embed async in async?

f#
I read the following code from the book Expert f#, Why the function collectLinks embeds let! html = async { .... } in the outer async block? How about just flat it by removing the inner async? Same question for the function waitForUrl in urlCollector which has a do! Async.StartChild...

## How to implement generics with type annotations in F#

generics,f#
I have the following skeleton of code: type MyException<'T> () = inherit Exception() type IMyInterface = abstract member Method<'T when 'T : (new: unit -> 'T) and 'T :> Exception> : string -> int type MyClass = interface IMyInterface with member this.Method s = let i = s.IndexOf "a" if...

.net,powershell,f#,system.reactive,f#-async
There is a subscription to an observable that sends out log messages. Some of the log messages come from other threads because they are are in F# async blocks. I need to be able to write out the messages from the main thread. Here is the code that currently filters...

## How to combine two seqs (not using Seq.append)

f#
type Message = | Str of string | KeyPress of ConsoleKeyInfo let keyStream = seq { while true do yield KeyPress(Console.ReadKey(true)) } let rec messageStream = keyStream |> Seq.combine (messageStream |> Seq.filter filterer) |> filterALot messageStream |> Seq.iter (fun _ -> ()) Here is my code. Basically I want to...

## Duplicate Key in index of Deedle Series

f#,deedle
I have a list of events that occur in a system. My goal is to take the list of events and create a sliding window of the series to determine rate event occurrences. The events are loaded into the events list from an application outside of this scope of the...

## FParsec parses only first half

parsing,f#,dsl,fparsec
In my AST, I have something like this: type Program = Declaration list and Declaration = | TypedefDeclaration of TypedefDeclaration and TypedefDeclaration = | Typedef1 of Name * TypeName | Typedef2 of Name * Name For parsing both of the statements, I have used something like this: let tdefpartial =...