Secondly, probably varying amounts of all of them. Primarily an object-oriented imperative language, Java is also an impure functional language. Although Processing doesn't support the syntax for Java 8 functions AFAIK.
In terms of practice, common examples, and the experience of programming:
In early example sketches Processing is generally introduced as simple imperative programming. There are objects underneath, but the beginning programmer does not address them and instead learns to make series of imperative statements that often involve assignment. For example, this is a valid Processing sketch written in an imperative style:
int x = 0;
x = x + 10;
println(x);
Early on Processing beginners also learn to define built-in processing "functions" like setup() and keyPressed() -- and later to create their own functions. Despite the name "functions" this is also imperative programming and is often focused primarily on the management of state.
While Processing sketches are always using objects (such as PApplet) behind the scenes, intermediate Processing programmers often begin using the built-in classes that come with the Processing core libraries, using new to create a new object such as a PVector or calling a builder function to return an object such as a PImage, PShape, or PGraphics etc.
Later, as they work on larger projects and advanced systems, Processing users also begin to use class to define their own classes of objects -- this is object oriented programming.
While it is possible to write a sketch in a functional programming style in Processing (or almost any language), and while it is true that Java 8 adds features to support functional programming, in practice Processing sketches are never purely functional and almost never even primarily functional, because:
a) almost every example sketch, tutorial, and library uses assignment, global variables, and/or side-effects
b) the entire Processing core library is object-oriented, so using Processing at all usually creates something that is OO at a low level almost by definition. For example, the draw() loop is fundamentally an imperative method of an object, and it's behavior changes based on global states (such as a looping bit, a frameRate speed, and whether the current frame is 0 or some other number). If you are using draw() you aren't doing pure functional programming.
While it is possible to write a sketch in a functional programming style
I have seen functional programming in p5.js and I guess it is common in js in general (Correct me if I am wrong). What are the advantages of this type of programming? Passing functions to objects (for example, listening to certain events), would qualify as functional programming as well?
I can imagine we don't have the option of choosing btween OO or functional programming most times. So now I am curious about functional programming in Processing.
So now I am curious about functional programming in Processing.
B/c p5.js is a JS library, we may try out a more functional approach there.
Python & CoffeeScript Modes can easily use the functional approach as well.
For Java Mode, at least for ".pde" tab files, we're mostly stuck in OOP due to PDE's pre-processor incompleteness. ~X(
For ".java" tab files, at least in my PDE version 3.1.2, even though internally our sketches are compiled by built-in Java 8, it is still configured to emit Java 7 instead: =(( https://GitHub.com/processing/processing/issues/4484
Conceptually, a functional interface has exactly one abstract method.
However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.
Given pre-Java 8 versions don't have lambdas and other functional features built-in, a hackish way of doing functional programming on those old versions is having an interface or abstract class to wrap an abstract method().
This way we can anonymous instantiate them by overriding their internal method w/ our own implementation, and store them in variables. *-:)
We need all that boilerplate b/c we can't store functions inside variables under pre-Java 8 versions.
That is, having some interface or class as a wrapper for our function is the only way to bypass the lack of function-datatype variables for old Javas. 8-|
This is how I've instantiated the Comparator, implemented its compare(), and stored the resultant object in a variable called BUBBLE_X_BY_DIAM:
static final Comparator<Bubble> BUBBLE_X_BY_DIAM = new Comparator<Bubble>() {
@Override final int compare(final Bubble a, final Bubble b) {
final int diff = a.d - b.d; // ascending order
if (diff > 0) { // swap fields x
final int tmp = a.x;
a.x = b.x;
b.x = tmp;
}
return diff;
}
};
B/c in pre-Java 8 we can't store the method compare() directly in a variable.
We need to wrap it up inside some object. :(|)
Now that we got BUBBLE_X_BY_DIAM representing the wrapped up compare() function, we can pass them to other functions as their argument! \m/
@SafeVarargs static final
<T> T[] bubbleSort(final Comparator<T> cmp, final T... arr)
throws NullPointerException {
int len = max(0, arr.length - 1);
while (len != 0) {
int i = 0, newLen = 0;
while (++i <= len) {
final T left = arr[i - 1], right = arr[i];
if (cmp.compare(left, right) > 0) {
arr[newLen = i - 1] = right;
arr[i] = left;
}
}
len = newLen;
}
return arr;
}
The bubbleSort()'s parameter final Comparator<T> cmp acts like a function variable.
And inside bubbleSort(), Comparator's compare() is called back in order to decide whether the 1st Bubble's field d is greater than the 2nd's: if (cmp.compare(left, right) > 0) {
This is the statement which invokes bubbleSort(): bubbleSort(BUBBLE_X_BY_DIAM, bubbles);
As you can see, we've just emulated functional programming using just old Java syntax! <:-P
Thanks @GoToLoop. I will be reviewing all these concepts and video(s). Really great illustrations of concept. One more question. Does this concept have a parallel in C/C++?
If you see processing in itself you can write fully non-oop sketches and you can write partially oop sketches. I don't think you can write fully oop since setup and draw are not oop.
In a broader view, processing capsules java and in this sense it is java and fully oop.
@GoToLoop it feels odd to mutate the arguments to your Comparator (in fact, had assumed it was against the spec). More to the point it stops it being an example of functional programming.
... it feels odd to mutate the arguments to your Comparator ...
You're right, that was an experimental hack. In my 1st attempt, those mutations were instead inside bubbleSort(), @ if (cmp.compare(left, right) > 0) { block. :-j
... it stops it being an example of functional programming.
A bold statement indeed! It's very true immutability belongs to the functional programming's universe.
1 of its tenets is passing around functions which can do some action or decision over some container.
Indeed my Comparator::compare() function implementation is supposed to be a predicate only.
That is, it should only make some testing to 2 passed objects.
But besides that, it betrays its prerogative and swaps their fields x too.
Whether that violation is enough to remove the functional programming's "title" completely, turning that implementation an outcast, pariah, heretic, etc. I'm not sure. /:)
I see it more like some hackish, unorthodox prank! ;))
I was just trying to demo how even a language w/o any functional built-in features, like the old Javas, can still use some of its concepts w/ some effort & boilerplating. >:)
A bold statement indeed! It's very true immutability belongs to the functional programming's universe.
1 of its tenets is passing around functions which can do some action or decision over some container.
If your example was functional the Bubble type would be final and immutable, and you'd return a new Bubble rather than change it. Likewise, in a functional paradigm you wouldn't sort the list in place, you'd have a function that returned a new sorted list and leaves the old list unmodified. You can see this in action in the implementation of streams in Java 8, but it's possible to code like this in older Java versions. Lambdas are not really needed to code in a functional way in Java, they just make it a lot nicer (and better performing), but you can do it with anonymous classes.
Incidentally, your example isn't a predicate - a predicate returns a boolean.
btw, nothing against your code - hackish and unorthodox is sometimes good! :-)
@GoToLoop a predicate returns true or false, yes or no (at least in the way it's used in functional programming). Yes, you can misuse Boolean to represent ternary logic, but the function is then no longer a predicate.
The Comparator isn't strictly ternary logic either, as the function is allowed to return any int value (something that is quite useful in practice). The way that return value is used could be seen as ternary logic.
Oh my after a day without logging in,ive never thought there would be a lot of replies, thank you very much guys i really appreciated your answers.
in addition with my question,
why is it some people say that processing is not a language? and is processing still considered as functional and object oriented language even it does not have a direct equivalent or keyword for Input? especially user input. for example if the user wants to input 2 numbers to add. :)
In addition to this, we obviously have the forum community, the developers and the excellent tutorials and websites. But they're not the software part of it.
I wanted to add these two articles that are related to this post. Mind the first one is about two years old but still relevant. To clarify about any terms of the article to current designs, please ask in the forum. The second article is informative and even discusses difference of processing and p5.js so it could be used as a reference in future posts.
Comments
Firstly, Processing is really just Java.
Secondly, probably varying amounts of all of them. Primarily an object-oriented imperative language, Java is also an impure functional language. Although Processing doesn't support the syntax for Java 8 functions AFAIK.
@akatsuki_bluecloud --
In terms of practice, common examples, and the experience of programming:
In early example sketches Processing is generally introduced as simple imperative programming. There are objects underneath, but the beginning programmer does not address them and instead learns to make series of imperative statements that often involve assignment. For example, this is a valid Processing sketch written in an imperative style:
Early on Processing beginners also learn to define built-in processing "functions" like
setup()
andkeyPressed()
-- and later to create their own functions. Despite the name "functions" this is also imperative programming and is often focused primarily on the management of state.While Processing sketches are always using objects (such as PApplet) behind the scenes, intermediate Processing programmers often begin using the built-in classes that come with the Processing core libraries, using
new
to create a new object such as a PVector or calling a builder function to return an object such as a PImage, PShape, or PGraphics etc.Later, as they work on larger projects and advanced systems, Processing users also begin to use
class
to define their own classes of objects -- this is object oriented programming.While it is possible to write a sketch in a functional programming style in Processing (or almost any language), and while it is true that Java 8 adds features to support functional programming, in practice Processing sketches are never purely functional and almost never even primarily functional, because:
a) almost every example sketch, tutorial, and library uses assignment, global variables, and/or side-effects
b) the entire Processing core library is object-oriented, so using Processing at all usually creates something that is OO at a low level almost by definition. For example, the
draw()
loop is fundamentally an imperative method of an object, and it's behavior changes based on global states (such as a looping bit, a frameRate speed, and whether the current frame is 0 or some other number). If you are using draw() you aren't doing pure functional programming.From the classes listed above, only PVector we use the
new
approach.The others we shouldn't! Instead their corresponding builder functions: L-)
createImage() or loadImage(), createShape() or loadShape(), createGraphics().
BtW, here's a very good article about Processing in general: :bz
http://www.MakeUseOf.com/tag/learn-program-processing-language-visual-designers/
@GoToLoop -- very true -- fixed that sentence above.
I have seen functional programming in p5.js and I guess it is common in js in general (Correct me if I am wrong). What are the advantages of this type of programming? Passing functions to objects (for example, listening to certain events), would qualify as functional programming as well?
I can imagine we don't have the option of choosing btween OO or functional programming most times. So now I am curious about functional programming in Processing.
Kf
Yes, they would! JS is multi-paradigm language after all. \m/
If you wanna know more about functional programming is JS, watch these YouTube channel videos: :bz
https://www.YouTube.com/channel/UCO1cgjhGzsSYb1rsB4bFe4Q
https://GitHub.com/processing/processing/issues/4484
But not all is lost for Java Mode, b/c functional programming is a problem-solving style rather than language features. >-)
Many Java interfaces are annotated w/
@FunctionalInterface
:http://docs.Oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
Given pre-Java 8 versions don't have lambdas and other functional features built-in, a hackish way of doing functional programming on those old versions is having an
interface
orabstract class
to wrap anabstract method()
.This way we can anonymous instantiate them by overriding their internal method w/ our own implementation, and store them in variables. *-:)
We need all that boilerplate b/c we can't store functions inside variables under pre-Java 8 versions.
That is, having some interface or class as a wrapper for our function is the only way to bypass the lack of function-datatype variables for old Javas. 8-|
Yesterday I've coded an awkward bubble sorting sample @:
https://forum.Processing.org/two/discussion/20601/bubblesort#Item_12
I've used the
@FunctionalInterface
Comparator as a way to emulate functional programming in PDE's Java Mode:http://docs.Oracle.com/javase/8/docs/api/java/util/Comparator.html
That interface got exactly 1
abstract method()
called compare():http://docs.Oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-T-T-
This is how I've instantiated the Comparator, implemented its compare(), and stored the resultant object in a variable called BUBBLE_X_BY_DIAM:
B/c in pre-Java 8 we can't store the method compare() directly in a variable.
We need to wrap it up inside some object. :(|)
Now that we got BUBBLE_X_BY_DIAM representing the wrapped up compare() function, we can pass them to other functions as their argument! \m/
The bubbleSort()'s parameter
final Comparator<T> cmp
acts like a function variable.And inside bubbleSort(), Comparator's compare() is called back in order to decide whether the 1st Bubble's field d is greater than the 2nd's:
if (cmp.compare(left, right) > 0) {
This is the statement which invokes bubbleSort():
bubbleSort(BUBBLE_X_BY_DIAM, bubbles);
As you can see, we've just emulated functional programming using just old Java syntax! <:-P
You can also compare the p5.js' take on that below: :-bd
https://forum.Processing.org/two/discussion/20601/bubblesort#Item_14
In place of the Java boilerplate implementation, compare() became just: $-)
@GoToLoop Very true. I am waiting for Processing to shift to Java 8 completely, but I think that they will now only shift to Java 10 once it comes.
Thanks @GoToLoop. I will be reviewing all these concepts and video(s). Really great illustrations of concept. One more question. Does this concept have a parallel in C/C++?
Kf
C got function pointers that can be stored in variables and passed to other functions. Although it's got a complex syntax: 8-X
http://www.GeeksForGeeks.org/function-pointer-in-c/
C++, in addition to C, got a more formal way to declare a function w/ function parameters: B-)
https://en.Wikipedia.org/wiki/Function_pointer#In_C.2B.2B
If you see processing in itself you can write fully non-oop sketches and you can write partially oop sketches. I don't think you can write fully oop since setup and draw are not oop.
In a broader view, processing capsules java and in this sense it is java and fully oop.
@GoToLoop it feels odd to mutate the arguments to your Comparator (in fact, had assumed it was against the spec). More to the point it stops it being an example of functional programming.
You're right, that was an experimental hack. In my 1st attempt, those mutations were instead inside bubbleSort(), @
if (cmp.compare(left, right) > 0) {
block. :-jA bold statement indeed! It's very true immutability belongs to the functional programming's universe.
1 of its tenets is passing around functions which can do some action or decision over some container.
Indeed my Comparator::compare() function implementation is supposed to be a predicate only.
That is, it should only make some testing to 2 passed objects.
But besides that, it betrays its prerogative and swaps their fields x too.
Whether that violation is enough to remove the functional programming's "title" completely, turning that implementation an outcast, pariah, heretic, etc. I'm not sure. /:)
I see it more like some hackish, unorthodox prank! ;))
I was just trying to demo how even a language w/o any functional built-in features, like the old Javas, can still use some of its concepts w/ some effort & boilerplating. >:)
Some key tenets of functional programming are the avoidance of changing state / mutable data, and the lack of side effects. eg. see https://en.wikipedia.org/wiki/Functional_programming
If your example was functional the
Bubble
type would be final and immutable, and you'd return a newBubble
rather than change it. Likewise, in a functional paradigm you wouldn't sort the list in place, you'd have a function that returned a new sorted list and leaves the old list unmodified. You can see this in action in the implementation of streams in Java 8, but it's possible to code like this in older Java versions. Lambdas are not really needed to code in a functional way in Java, they just make it a lot nicer (and better performing), but you can do it with anonymous classes.Incidentally, your example isn't a predicate - a predicate returns a
boolean
.btw, nothing against your code - hackish and unorthodox is sometimes good! :-)
Indeed it's got 3 states: negative, zero & positive.
It can be represented in Boolean as:
false
,null
&true
: :Phttp://docs.Oracle.com/javase/8/docs/api/java/lang/Boolean.html
@GoToLoop a predicate returns true or false, yes or no (at least in the way it's used in functional programming). Yes, you can misuse
Boolean
to represent ternary logic, but the function is then no longer a predicate.The Comparator isn't strictly ternary logic either, as the function is allowed to return any
int
value (something that is quite useful in practice). The way that return value is used could be seen as ternary logic.Oh my after a day without logging in,ive never thought there would be a lot of replies, thank you very much guys i really appreciated your answers. in addition with my question, why is it some people say that processing is not a language? and is processing still considered as functional and object oriented language even it does not have a direct equivalent or keyword for Input? especially user input. for example if the user wants to input 2 numbers to add. :)
Processing, under the hood, is just-
In addition to this, we obviously have the forum community, the developers and the excellent tutorials and websites. But they're not the software part of it.
There are 3rd libraries for that, like G4P & ControlP5 for example: O:-)
https://Processing.org/reference/libraries/#gui
Or we can write 1 by ourselves: #:-S
https://forum.Processing.org/two/discussion/13175/do-whille-is-not-working-how-it-suppose-to#Item_12
https://forum.Processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java
@Lord_of_the_Galaxy and @GoToLoop oh okay Thank you very much, and I will try those programs you gave.
I wanted to add these two articles that are related to this post. Mind the first one is about two years old but still relevant. To clarify about any terms of the article to current designs, please ask in the forum. The second article is informative and even discusses difference of processing and p5.js so it could be used as a reference in future posts.
Limits of Processing
My experience with Processing
******EDIT: Next added:
Additional post: https://medium.com/@ProcessingOrg/thoughts-on-software-a8a82c95e1ad#.tturmw8ol
https://p5js.org/tutorials/
https://github.com/processing/p5.js/wiki/Processing-transition
Kf