How to pass function as parameter


epinephrine

I have a function that takes a vector and sums all elements.

(def rec
  (fn [numbers acc]
    (if (empty? numbers)
      acc
      (recur (rest numbers) (+ acc (first numbers))))))
(prn (rec [1 2 3] 0))

However, instead of calling the function "+", I don't want to pass a function as a parameter, and pass a function as a parameter and then call that function.

I tried:

(def rec
  (fn [f numbers acc]
    (if (empty? numbers)
      acc
      (recur (rest numbers) (f acc (first numbers))))))
(prn (rec + [4 2 1] 0))

But this doesn't work, I know there is a better way to sum numbers in a vector, but I'm starting with a function, so it's important to do this exercise.

Thanks in advance.

Diego Basch

In this case you need to recurse with the same parameters as the parameter vector:

(recur f (rest numbers) (f acc (first numbers))))))

(By the way, the standard defnused to define a function (defn f[x] ... )is(def f (fn [x] ...)))

Related


How to pass function as parameter

epinephrine I have a function that takes a vector and sums all elements. (def rec (fn [numbers acc] (if (empty? numbers) acc (recur (rest numbers) (+ acc (first numbers)))))) (prn (rec [1 2 3] 0)) However, instead of calling the function "+"

How to pass function as parameter

epinephrine I have a function that takes a vector and sums all elements. (def rec (fn [numbers acc] (if (empty? numbers) acc (recur (rest numbers) (+ acc (first numbers)))))) (prn (rec [1 2 3] 0)) However, instead of calling the function "+"

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass function as parameter to parameter?

Java Prophet So I'm using callbacks in GNUTLS. I want to pass my function handleSNIto the function, but I also want to pass some other important parameters (to it when calling SNI). I can execute them outside the function of the global variable, but this is no

How to pass a class as a function parameter

xypher: I know this question has been asked before, but I don't quite understand/the answer is not helpful, so as I start learning Java I'd like some help in this regard. I want to create a function that calculates an instance of a class and returns me a numbe

How to pass function as parameter in Java?

Jason: In Java, how do I pass a function as an argument to another function? jk。: Java 8 and above If your class or interface has only one abstract method (sometimes called a SAM type ), use a Java 8+ lambda expression like: public interface MyInterface {

How to pass a type as a function parameter

Isaac I have the following type checking code to verify that userInput is one of the defined values const variantColorValues = ['primary', 'black', 'green', 'red', 'white'] as const; export type VariantColor = (typeof variantColorValues)[number]; function isO

How to pass a struct as a parameter to a function

Mirabeau: I have several configuration JSON files, each with a specific struc type Currently, I create a function for each file/struct name: type ConfigOne struct { App string `json:"app"` Web string `json:"web"` Archive string `json:"archi

How to pass function as parameter in Java

you must: I have a matrix class and I want to create a function called map in this class that looks like this public void map(T fn) { //do something } The idea behind the function is to take as parameter fn which can be any method and apply this me

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

How to pass Backbone function as parameter

Dan Dan I have a requirement to pass a function in a backbone view into another function in the same view. I used the following method which works perfectly for global functions. However, when it comes to Backbone view instances, it doesn't work. I believe the

How to pass function as parameter in Java?

Jason: In Java, how do I pass a function as an argument to another function? jk。: Java 8 and above If your class or interface has only one abstract method (sometimes called a SAM type ), use a Java 8+ lambda expression like: public interface MyInterface {

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass a tuple as a function parameter

AJcodez Got a function with three parameters. f(a, b, c) = # do stuff There is another function that returns a tuple. g() = (1, 2, 3) How to pass a tuple as a function parameter? f(g()) # ERROR Guillermo Garza Using Nanashi's example, the hint is that you m

How to pass a struct as a parameter to a function?

mess How to pass struct to use as parameter in golang? There is my code: package main import ( "fmt" ) type MyClass struct { Name string } func test(class interface{}) { fmt.Println(class.Name) } func main() { test(MyClass{Name: "Jhon"}) }

How to pass function as context parameter?

Truman 1 I want to pass a delegate in the context and try to do something like this: func process(value: Float) -> Float { return value * 999.9 } ----------- self.pushControllerWithName("someController", context: [ "func": SomeUtils

How to pass template as parameter to function

Goodfellas 95 // Example program #include <iostream> #include <string> #include <array> using namespace std; int returnSize(template z <class T, size_t>) { /*if(arr.size() ==0) return 1; else return 2; */ return 1; } int main() {

How to pass a struct as a parameter to a function

Mirabeau: I have several configuration JSON files, each with a specific struc type Currently, I create a function for each file/struct name: type ConfigOne struct { App string `json:"app"` Web string `json:"web"` Archive string `json:"archi

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

How to pass prototype function as parameter?

intellectuals Let say: function MapMePls (str, func, ...args) { return str.func(...args); } MapMePls('Hello World!', toLowerCase); Said funccan come from any prototype function String. Suren Srapian You can pass the name of the function as stringand use th

How to pass function as parameter in Java

you must: I have a matrix class and I want to create a function called map in this class that looks like this public void map(T fn) { //do something } The idea behind the function is to take as parameter fn which can be any method and apply this me

How to pass a type as a function parameter

Isaac I have the following type checking code to verify that userInput is one of the defined values const variantColorValues = ['primary', 'black', 'green', 'red', 'white'] as const; export type VariantColor = (typeof variantColorValues)[number]; function isO

How to pass an array parameter to a function

Peter XX I have implemented a simple merge sort function in PowerShell as follows function Merge-Sort { param($a) if ($a.Length -gt 1) { $m = [Math]::floor($a.Length / 2) [Int[]]$l = $a[0..($m-1)] [Int[]]$r = $a[$m..($a.Length)] Merge-So

How to pass dictionary as function parameter?

Alex I am trying to pass a dictionary as a function parameter. I have the following function func makeAndAddVisitorRecord2(visitorDict: Dictionary) -> ABRecordRef <AnyObject, AnyObject> { let visitorRecord: ABRecordRef = ABPersonCreate().takeRetainedValue(

How to pass function as optional parameter

Tosi New to Swift, I'm currently passing functions as optional parameters via: import Foundation func foo() { print("Foo") } func bar() { print("Bar") } func dummy() { return } func myFunc() { myFunc(callBack: dummy()) } func myFunc(callB

How to pass a struct * as a parameter to a function

unknown I want to pass alloc as a parameter but don't know how, can anyone help me? void parameter(unsigned int argnum, struct resistor* alloc) { /*...*/ } struct resistort { const double E6[6]; double E12[12]; const double E24[24]; char e[3]; doubl

How to pass NSDateComponent as function parameter

Ralf Lukner, MD How to pass NSDateComponent object or reference as function parameter (read only)? The following code fails "compile" with a "Use of undeclared type 'NSDateComponent'" error: import Foundation class PreventativeCare { var birthday = NSDateC

How to pass activity as parameter in function

Akio I want to create a function that receives an activity as a parameter and change it to that activity. However, if I use .classthe Activity's calling function, it will change the function's parameter to Class<{Name}Activity>, so it will only accept that act