How to create an inline anonymous function, for immediate use in a function call?
I have this Swift function which I created which takes a function as a param:
func doMath(_ f:(_ i1 : Int, _ i2 : Int) ->
(), _ i1: Int, _ i2: Int) {
print("doing Math")
f(i1, i2)
}
The function takes two params (both of Int) and returns nothing.
I can successfully call that method with the following code, using a non-anonymous function.
func add(_ m1:Int, _ m2: Int){
print (m1 + m2)
}
doMath(add, 3,5)
When doMath is called, it prints:
doing Math
and then calls the add function,
which then prints:
8
What syntax allows calling the doMath
function with an anonymous function?
What I've Tried
- I'm reading the book, iOS 12 Programming Fundamentals with Swift by Matt Neuberg but he skips explaining anonymous methods with params.
- I've googled and SOed but can't find a swift example that I can figure out.
- I've tried many variations and the following is my closest but I get the error shown below:
doMath(5,8, {
(m1:Int, m2:Int) -> () in
print(m1 * m2)
})
I get an error that states:
swift syntax parameters anonymous-function
add a comment |
I have this Swift function which I created which takes a function as a param:
func doMath(_ f:(_ i1 : Int, _ i2 : Int) ->
(), _ i1: Int, _ i2: Int) {
print("doing Math")
f(i1, i2)
}
The function takes two params (both of Int) and returns nothing.
I can successfully call that method with the following code, using a non-anonymous function.
func add(_ m1:Int, _ m2: Int){
print (m1 + m2)
}
doMath(add, 3,5)
When doMath is called, it prints:
doing Math
and then calls the add function,
which then prints:
8
What syntax allows calling the doMath
function with an anonymous function?
What I've Tried
- I'm reading the book, iOS 12 Programming Fundamentals with Swift by Matt Neuberg but he skips explaining anonymous methods with params.
- I've googled and SOed but can't find a swift example that I can figure out.
- I've tried many variations and the following is my closest but I get the error shown below:
doMath(5,8, {
(m1:Int, m2:Int) -> () in
print(m1 * m2)
})
I get an error that states:
swift syntax parameters anonymous-function
Your last attempt is almost right, you only got the order of parameters wrong:doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
You definedfunc doMath
taking a closure as the first argument, and two integers as second and third argument.
– Martin R
Nov 20 '18 at 19:12
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14
add a comment |
I have this Swift function which I created which takes a function as a param:
func doMath(_ f:(_ i1 : Int, _ i2 : Int) ->
(), _ i1: Int, _ i2: Int) {
print("doing Math")
f(i1, i2)
}
The function takes two params (both of Int) and returns nothing.
I can successfully call that method with the following code, using a non-anonymous function.
func add(_ m1:Int, _ m2: Int){
print (m1 + m2)
}
doMath(add, 3,5)
When doMath is called, it prints:
doing Math
and then calls the add function,
which then prints:
8
What syntax allows calling the doMath
function with an anonymous function?
What I've Tried
- I'm reading the book, iOS 12 Programming Fundamentals with Swift by Matt Neuberg but he skips explaining anonymous methods with params.
- I've googled and SOed but can't find a swift example that I can figure out.
- I've tried many variations and the following is my closest but I get the error shown below:
doMath(5,8, {
(m1:Int, m2:Int) -> () in
print(m1 * m2)
})
I get an error that states:
swift syntax parameters anonymous-function
I have this Swift function which I created which takes a function as a param:
func doMath(_ f:(_ i1 : Int, _ i2 : Int) ->
(), _ i1: Int, _ i2: Int) {
print("doing Math")
f(i1, i2)
}
The function takes two params (both of Int) and returns nothing.
I can successfully call that method with the following code, using a non-anonymous function.
func add(_ m1:Int, _ m2: Int){
print (m1 + m2)
}
doMath(add, 3,5)
When doMath is called, it prints:
doing Math
and then calls the add function,
which then prints:
8
What syntax allows calling the doMath
function with an anonymous function?
What I've Tried
- I'm reading the book, iOS 12 Programming Fundamentals with Swift by Matt Neuberg but he skips explaining anonymous methods with params.
- I've googled and SOed but can't find a swift example that I can figure out.
- I've tried many variations and the following is my closest but I get the error shown below:
doMath(5,8, {
(m1:Int, m2:Int) -> () in
print(m1 * m2)
})
I get an error that states:
swift syntax parameters anonymous-function
swift syntax parameters anonymous-function
edited Nov 20 '18 at 19:15
user2864740
44k671149
44k671149
asked Nov 20 '18 at 19:06
raddevusraddevus
2,87243343
2,87243343
Your last attempt is almost right, you only got the order of parameters wrong:doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
You definedfunc doMath
taking a closure as the first argument, and two integers as second and third argument.
– Martin R
Nov 20 '18 at 19:12
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14
add a comment |
Your last attempt is almost right, you only got the order of parameters wrong:doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
You definedfunc doMath
taking a closure as the first argument, and two integers as second and third argument.
– Martin R
Nov 20 '18 at 19:12
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14
Your last attempt is almost right, you only got the order of parameters wrong:
doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Your last attempt is almost right, you only got the order of parameters wrong:
doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
You defined
func doMath
taking a closure as the first argument, and two integers as second and third argument.– Martin R
Nov 20 '18 at 19:12
You defined
func doMath
taking a closure as the first argument, and two integers as second and third argument.– Martin R
Nov 20 '18 at 19:12
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14
add a comment |
1 Answer
1
active
oldest
votes
func doMath
– as you defined it – takes a closure as the first argument,
and two integers as second and third argument. Therefore you call it as
doMath({
(m1:Int, m2:Int) -> () in
print(m1 * m2)
}, 5, 8)
or with shorthand parameters:
doMath({ print($0 * $1) }, 5, 8)
If you change the function definition to take the closure as the last
parameter
func doMath(_ i1: Int, _ i2: Int, _ f:(_ i1 : Int, _ i2 : Int) -> ()) {
print("doing Math")
f(i1, i2)
}
then you would call it as
doMath(5, 8, { print($0 * $1) })
or, using the “trailing closure” syntax:
doMath(5, 8) { print($0 * $1) }
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399886%2fhow-to-create-an-inline-anonymous-function-for-immediate-use-in-a-function-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
func doMath
– as you defined it – takes a closure as the first argument,
and two integers as second and third argument. Therefore you call it as
doMath({
(m1:Int, m2:Int) -> () in
print(m1 * m2)
}, 5, 8)
or with shorthand parameters:
doMath({ print($0 * $1) }, 5, 8)
If you change the function definition to take the closure as the last
parameter
func doMath(_ i1: Int, _ i2: Int, _ f:(_ i1 : Int, _ i2 : Int) -> ()) {
print("doing Math")
f(i1, i2)
}
then you would call it as
doMath(5, 8, { print($0 * $1) })
or, using the “trailing closure” syntax:
doMath(5, 8) { print($0 * $1) }
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
add a comment |
func doMath
– as you defined it – takes a closure as the first argument,
and two integers as second and third argument. Therefore you call it as
doMath({
(m1:Int, m2:Int) -> () in
print(m1 * m2)
}, 5, 8)
or with shorthand parameters:
doMath({ print($0 * $1) }, 5, 8)
If you change the function definition to take the closure as the last
parameter
func doMath(_ i1: Int, _ i2: Int, _ f:(_ i1 : Int, _ i2 : Int) -> ()) {
print("doing Math")
f(i1, i2)
}
then you would call it as
doMath(5, 8, { print($0 * $1) })
or, using the “trailing closure” syntax:
doMath(5, 8) { print($0 * $1) }
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
add a comment |
func doMath
– as you defined it – takes a closure as the first argument,
and two integers as second and third argument. Therefore you call it as
doMath({
(m1:Int, m2:Int) -> () in
print(m1 * m2)
}, 5, 8)
or with shorthand parameters:
doMath({ print($0 * $1) }, 5, 8)
If you change the function definition to take the closure as the last
parameter
func doMath(_ i1: Int, _ i2: Int, _ f:(_ i1 : Int, _ i2 : Int) -> ()) {
print("doing Math")
f(i1, i2)
}
then you would call it as
doMath(5, 8, { print($0 * $1) })
or, using the “trailing closure” syntax:
doMath(5, 8) { print($0 * $1) }
func doMath
– as you defined it – takes a closure as the first argument,
and two integers as second and third argument. Therefore you call it as
doMath({
(m1:Int, m2:Int) -> () in
print(m1 * m2)
}, 5, 8)
or with shorthand parameters:
doMath({ print($0 * $1) }, 5, 8)
If you change the function definition to take the closure as the last
parameter
func doMath(_ i1: Int, _ i2: Int, _ f:(_ i1 : Int, _ i2 : Int) -> ()) {
print("doing Math")
f(i1, i2)
}
then you would call it as
doMath(5, 8, { print($0 * $1) })
or, using the “trailing closure” syntax:
doMath(5, 8) { print($0 * $1) }
answered Nov 20 '18 at 19:19
Martin RMartin R
400k56884985
400k56884985
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
add a comment |
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
Thanks this got me to the place I needed to be and gave me additional information that is very valuable so I could understand other concepts more clearly.
– raddevus
Nov 20 '18 at 19:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399886%2fhow-to-create-an-inline-anonymous-function-for-immediate-use-in-a-function-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your last attempt is almost right, you only got the order of parameters wrong:
doMath({ .... }, 5,8)
– Martin R
Nov 20 '18 at 19:10
Oh, wait, the body comes first and then the params!?! I've tried everything, except that. Never would've tried it. I'll try it now. EDIT - Tried it and it worked !!! That syntax!! I kind of hate it. But maybe I'll love it later. xD Argh!
– raddevus
Nov 20 '18 at 19:11
You defined
func doMath
taking a closure as the first argument, and two integers as second and third argument.– Martin R
Nov 20 '18 at 19:12
@MartinR Oh, it's the way I defined the other method. All because I'm learning Swift and attempting to create samples. Okay, write those two things up in an answer and I'm happy to mark as answer. Meanwhile, I'll go back and re-read Neuberg's explanations too.
– raddevus
Nov 20 '18 at 19:14