Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define an F# operator that applies multiple functions to a single argument (almost the opposite of the ||> operator)?

Tags:

f#

My attempt to do this is here (forgive the for loop - I was just curious to see if this was possible):

let (|>>) a (b : ('a -> unit) list) = 
    for x in b do
        x a

but when I try to use it I get the error enter image description here

like image 948
Mark R Avatar asked Aug 16 '18 11:08

Mark R


People also ask

What does it mean if f is defined?

"f(a) is defined" means f is a function, and a is in the domain of f. For example suppose f(x)=1x. f(1) is defined.

When f is not defined?

Since you cannot divide by 0, the function is not defined when x=a. The function is undefined at a.

How do you prove that f is one to one?

To prove a function is One-to-One To prove f:A→B is one-to-one: Assume f(x1)=f(x2) Show it must be true that x1=x2. Conclude: we have shown if f(x1)=f(x2) then x1=x2, therefore f is one-to-one, by definition of one-to-one.

What is f'n n?

The function f:N → N ( N is the set of natural numbers) defined by f(n) = 2n + 3 is. SolveStudyTextbooksGuides.


2 Answers

That None of the types error message can occur if the function you're trying to use is defined further down the file or isn't imported correctly. Otherwise, your function definition seems ok.

like image 166
Chad Gilbert Avatar answered Nov 15 '22 12:11

Chad Gilbert


I would discourage the use of a custom operator for this. I think they should be used very rarely. This one doesn't seem general enough to be worth defining and could make code hard to read. Here is one alternative:

[ printf "%A"; printfn "%A" ] |> List.iter ((|>) 1)

But it's even clearer and shorter to write out your operator definition inline:

for f in [ printf "%A"; printfn "%A" ] do f 1
like image 41
TheQuickBrownFox Avatar answered Nov 15 '22 10:11

TheQuickBrownFox