Skip to content

Comments

Andrews blog post#13

Open
andrewcassidy wants to merge 3 commits intomasterfrom
andrews_blog_post
Open

Andrews blog post#13
andrewcassidy wants to merge 3 commits intomasterfrom
andrews_blog_post

Conversation

@andrewcassidy
Copy link
Collaborator

No description provided.

@andrewcassidy
Copy link
Collaborator Author

http://en.wikipedia.org/wiki/Partial_application.

A "partial function" in scala is completely different than a partially applied function. It is as you said a function that applies only to a certain domain. For example
5 match {
case x: Int if(x >5 ) => "greater than 5"
}

In the above code, match is "I believe" shorthand for an anonymous partial function. In this case it would return a match error since the input (the integer 5) would match none of the cases.

The term "partial application" I believe is universal and not specific to R. It sounds like this could be a good blog post both for the content, to clear up vocabulary, and address the different approaches used by different languages. Here's a blog post on it in javascript: http://ejohn.org/blog/partial-functions-in-javascript/. Do ya'll have a C# equivalent? I believe Steven said he was familiar with the concept

@madelson
Copy link
Collaborator

madelson commented Apr 3, 2014

In C#, you have anonymous functions that support closures, so you can do this. For example:

Func<int, int, int> add = (x, y) => x + y;
var specificX = 20;
Func add2 = y => add(specificX, y);

You can also write an extension method on the function type to provide direct currying similar to the javascript article you mentioned, but people don't tend to do this, probably because the parameter ordering constraints of currying means you end up going with the above style a good portion of the time anyway and it doesn't save many characters.

@andrewcassidy
Copy link
Collaborator Author

@madelson for R, javascript, and C#, partial application is a particular use of closure. Closure just means that when passing around a function, it will also pass a referencing environment for the "closed" variables.

// a simple closure in javascript
var a = 5
addFive = function(x) { return(x + a) }
addFive(5) // Should return 10
a = 6
addFive(5) // Should return 11

Can you add some C# examples to the blog post?

@andrewcassidy
Copy link
Collaborator Author

Also just to note I realized you can do this the old fashion way in scala too:

scala> def add(x: Int, y: Int) = x + y
add: (x: Int, y: Int)Int

scala> def addFive(x: Int) = {
| add(x, 5)
| }
addFive: (x: Int)Int

scala> addFive(6)
res0: Int = 11

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the wording "a basic example." I think you can more directly speak to the reader by saying something like "let's take a look at a classic example of partial functions." Also, I think it'd be clearer to explain the function we start with and the function we create.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include C# in the list of languages. It's a features that's used often in C# too...even if it's not as functional as scala.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants