Understanding Closures.. hard.. and Functions. (spec: Functions abstract over executable actions)
This conversation is from:
https://groups.google.com/a/dartlang.org/forum/?fromgroups=#!topic/misc/NOuoGrxW2nE
https://groups.google.com/a/dartlang.org/d/msg/misc/NOuoGrxW2nE/DHTVT-_qq40J
Direct link to just this piece of conversation.. Great!!
Alex Tatumizer Mar 14 (16 hours ago)
@Ladislav:
Somehow, I overlooked the fact that whatever is called Function in dart in fact doesn't correspond to javascript notion of Function - it's really a closure, e.g.
Function f="abcd".substring;
print(f(0,2));
This is a great feature, so I withdraw my sarcasm. I thought dart makes distinction between Function (as pointer to function "codes") and Closure, but it probably doesn't.
Won't it be better to call it Closure, not Function then, to avoid confusion?
Ladislav Thon asvered:
Function is just something that can be called. Even this, I believe:
class Answer {
call() => 42;
}
main() {
var a = new Answer();
print(a());
print(a is Function);
}
Some Functions are in fact closures, meaning that they carry their lexical environment, but not all of them. I'm not sure about renaming Function to Closure, I think it would make some situations more clear and some less.
Tatumizer ansvered:
Probably you are right, but then things get a bit confusing indeed for anyone coming from javascript (huge army of potential dart users).
Look at "Tour of the dart language" - they are talking about functions AND closures, without elaborating on subtleties - but this are very important subtleties, deserved to be featured much more prominently.
Needs to be better documented maybe?
Check link for more conversation...
And next theme: Orthogonal Wikipedia:
Computer scienceOrthogonality in programming language design is the ability to use various language features in arbitrary combinations with consistent results.[9] This usage was introduced by von Wijngaarten in the design of Algol 68:
The number of independent primitive concepts has been minimized in order that the language be easy to describe, to learn, and to implement. On the other hand, these concepts have been applied “orthogonally” in order to maximize the expressive power of the language while trying to avoid deleterious superfluities.[10]
Orthogonality is a system design property which guarantees that modifying the technical effect produced by a component of a system neither creates nor propagates side effects to other components of the system. Typically this is achieved through the separation of concerns and encapsulation, and it is essential for feasible and compact designs of complex systems. The emergent behavior of a system consisting of components should be controlled strictly by formal definitions of its logic and not by side effects resulting from poor integration, i.e. nonorthogonal design of modules and interfaces. Orthogonality reduces testing and development time because it is easier to verify designs that neither cause side effects nor depend on them.
An instruction set is said to be orthogonal if it lacks redundancy (i.e. there is only a single instruction that can be used to accomplish a given task)[11] and is designed such that instructions can use any register in any addressing mode. This terminology results from considering an instruction as a vector whose components are the instruction fields. One field identifies the registers to be operated upon, and another specifies the addressing mode. An orthogonal instruction setuniquely encodes all combinations of registers and addressing modes.[citatio
Published originally in etdart.blogspot.fi
Some useful Dart notes and Copy-Pastes from Dartlang.org. And some Hard-Spots of language questions.
Friday, March 15, 2013
Friday, April 20, 2012
Generics in Dart: <> merkkien käyttöä, T, E , DART:issa
This article is fully to be modified
Lisäksi DART kielessä on näitä <T> <E> ilmaisuja. Muiden sulkumerkkien jälkeen ne pysähdyttävät hieman miettimään... Miksi juuri T ja E ?
(K,V) Tarkoittaa siis Key ja Value, sehän on selvää.
Kokoan leikkeitä ohjeista:
http://www.dartlang.org/language-tour/#generics
Generics
If you look at the API documentation for the basic array type, List, you'll see that the type is actuallyList<E>. The <...> notation marks List as a generic (or parameterized) type—a type that can declare formal type parameters.
For example, if you intend for a list to contain only strings, you can declare it as List<String> (read that as "List of String").
....
Generic types can save you the trouble of creating all these interfaces. Instead, you can create a single interface that takes a type parameter:
interface Cache<T> {
T getByKey(String key);
setByKey(String key, T value);
}
In this code, T is the stand-in type. It's a placeholder that you can think of as a type that a developer will define later.
Using collection literals
Both built-in collection types are parameterized: lists and maps. Parameterized literals are just like the literals you've already seen, except that you add <type> before the opening bracket. For example:
List<String> names = <String>['Seth', 'Kathy', 'Lars'];
Map<String, String> pages = <String>{ // specify value type: String
'index.html':'Homepage', // (the key type is implicitly String)
'robots.txt':'Hints for web robots',
'humans.txt':'We are people, not machines' };
NO? Näyttää siltä siis, että <> merkinnän sisällä on "tyyppi"...
..
To specify one or more types when using a constructor, put the types in angle brackets (<...>
..
Generic collections and the types they contain
Dart generic types are reified, which is a fancy way of saying that they carry their type information around at runtime.
= = = = = = = = = = = = = = = = = = = = = = = = =
http://api.dartlang.org/dart_core.html
Collection<E>
HashSet<E>
Iterable<E>
Iterator<E>
List<E>
Queue<E>
Set<E>
HashMap<K, V>
LinkedHashMap<K, V>
Map<K, V> mapping a key to a value.
Completer<T>
Future<T>
SIIS: E T K V
What is this T in <T> ?
Interface Future<T>
A Future is used to obtain a value sometime in the future. Receivers of a Future obtain the value by passing a callback to then.
========================================================
What is this E in <E> ?
===========================================
http://www.dartlang.org/language-tour/#Typedefs
Typedefs can be parameterized.
typedef int Compare<T>(T a, T b);
class SortedCollection<T> {
Compare<T> compare;
SortedCollection(this.compare);
}
main() {
SortedCollection<int> s = new SortedCollection<int>((a,b) => a - b);
print(s.compare is Compare<int>); // true
print(s.compare('a','b')); // checked mode throws exception
}
dartlang 0.08
typeParameter:
identifier (extends type)?
;
typeParameters:
`<' typeParameter (`,' typeParameter)* `>'
;
A type parameter T may be suffixed with an extends clause that specifies
the upper bound for T.
A type parameter T may be suffixed with an extends clause that specifies
the upper bound for T. ....
Tässäpä hieman sulattelemista... Täydennän tekstiä vielä...
http://c.dart-examples.com/learn/variables/collections
Tämä kirjoitus tuo hieman lisävalaistusta asiaan...
.published originally in etdart.blogspot.fi
Lisäksi DART kielessä on näitä <T> <E> ilmaisuja. Muiden sulkumerkkien jälkeen ne pysähdyttävät hieman miettimään... Miksi juuri T ja E ?
(K,V) Tarkoittaa siis Key ja Value, sehän on selvää.
Kokoan leikkeitä ohjeista:
http://www.dartlang.org/language-tour/#generics
Generics
If you look at the API documentation for the basic array type, List, you'll see that the type is actuallyList<E>. The <...> notation marks List as a generic (or parameterized) type—a type that can declare formal type parameters.
For example, if you intend for a list to contain only strings, you can declare it as List<String> (read that as "List of String").
....
Generic types can save you the trouble of creating all these interfaces. Instead, you can create a single interface that takes a type parameter:
interface Cache<T> {
T getByKey(String key);
setByKey(String key, T value);
}
In this code, T is the stand-in type. It's a placeholder that you can think of as a type that a developer will define later.
Using collection literals
Both built-in collection types are parameterized: lists and maps. Parameterized literals are just like the literals you've already seen, except that you add <type> before the opening bracket. For example:
List<String> names = <String>['Seth', 'Kathy', 'Lars'];
Map<String, String> pages = <String>{ // specify value type: String
'index.html':'Homepage', // (the key type is implicitly String)
'robots.txt':'Hints for web robots',
'humans.txt':'We are people, not machines' };
NO? Näyttää siltä siis, että <> merkinnän sisällä on "tyyppi"...
..
To specify one or more types when using a constructor, put the types in angle brackets (<...>
..
Generic collections and the types they contain
Dart generic types are reified, which is a fancy way of saying that they carry their type information around at runtime.
= = = = = = = = = = = = = = = = = = = = = = = = =
http://api.dartlang.org/dart_core.html
Collection<E>
HashSet<E>
Iterable<E>
Iterator<E>
List<E>
Queue<E>
Set<E>
HashMap<K, V>
LinkedHashMap<K, V>
Map<K, V> mapping a key to a value.
Completer<T>
Future<T>
SIIS: E T K V
What is this T in <T> ?
Interface Future<T>
A Future is used to obtain a value sometime in the future. Receivers of a Future obtain the value by passing a callback to then.
========================================================
What is this E in <E> ?
===========================================
http://www.dartlang.org/language-tour/#Typedefs
Typedefs can be parameterized.
typedef int Compare<T>(T a, T b);
class SortedCollection<T> {
Compare<T> compare;
SortedCollection(this.compare);
}
main() {
SortedCollection<int> s = new SortedCollection<int>((a,b) => a - b);
print(s.compare is Compare<int>); // true
print(s.compare('a','b')); // checked mode throws exception
}
dartlang 0.08
typeParameter:
identifier (extends type)?
;
typeParameters:
`<' typeParameter (`,' typeParameter)* `>'
;
A type parameter T may be suffixed with an extends clause that specifies
the upper bound for T.
A type parameter T may be suffixed with an extends clause that specifies
the upper bound for T. ....
Tässäpä hieman sulattelemista... Täydennän tekstiä vielä...
http://c.dart-examples.com/learn/variables/collections
Tämä kirjoitus tuo hieman lisävalaistusta asiaan...
.published originally in etdart.blogspot.fi
Tuesday, March 27, 2012
Overloading, what do'esw it mean? mitä se tarkoittaa, DART ym. kielet
Like most dynamically-typed languages, Dart doesn't support overloading....
http://japhr.blogspot.com/2012/02/overloading-dart-operators-for-great.html
Overloading Dart Operators for Great Evil
http://en.wikipedia.org/wiki/Overloading
Constructor and function/method overloading, in computer science, a type of polymorphism where different functions with the same name are invoked based on the data types of the parameters passed
Operator overloading, a form of functional or method overloading where the action being overloaded is an operator, such as + or -
http://www.parashift.com/c++-faq-lite/operator-overloading.html
It allows you to provide an intuitive interface to users of your class, plus makes it possible for templates to work equally well with classes and built-in/intrinsic types.
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls...
Ja tähän Dart-kielestä esimerkkejä...
Function Overloading - General Dart Discussion | Google Groups
Oct 30, 2011 ... I'm progamming complex object and I am a little dissapointed with some aspect like it haven't got Methods Overloading. I think here could be ...
LabeledDiscussions
Constructor overloading - General Dart Discussion | Google Groups
Feb 12, 2012 ... Subject: Constructor overloading ... Why Dart doesn't allow to overloadconstructors. I have to define ... Subject: Re: Constructor overloading ...
LabeledDiscussions
Operator Overloading: "Convert" Operator - General Dart Discussion ...
Feb 13, 2012 ... I have seen this in other languages as an overloading of the "Convert" operator. Is this possible in Dart? For example: class Material{ ... } ...
LabeledDiscussions
http://www.dartlang.org/articles/idiomatic-dart/
Named constructors
Like most dynamically-typed languages, Dart doesn't support overloading. With methods, this isn't much of a limitation because you can always use a different name, but constructors aren't so lucky. To alleviate that, Dart lets you define named constructors:
class Point {
num x, y;
Point(this.x, this.y);
Point.zero() : x = 0, y = 0;
Point.polar(num theta, num radius) {
x = Math.cos(theta) * radius;
y = Math.sin(theta) * radius;
}
}
Here our Point class has three constructors, a normal one and two named ones. You can use them like so:
var a = new Point(1, 2);
var b = new Point.zero();
var c = new Point.polar(Math.PI, 4.0);
Note that we're still using new here when we invoke the named constructor. It isn't just a static method.
. originally published in etdart.blogspot.fi
http://japhr.blogspot.com/2012/02/overloading-dart-operators-for-great.html
Overloading Dart Operators for Great Evil
http://en.wikipedia.org/wiki/Overloading
Constructor and function/method overloading, in computer science, a type of polymorphism where different functions with the same name are invoked based on the data types of the parameters passed
Operator overloading, a form of functional or method overloading where the action being overloaded is an operator, such as + or -
http://www.parashift.com/c++-faq-lite/operator-overloading.html
It allows you to provide an intuitive interface to users of your class, plus makes it possible for templates to work equally well with classes and built-in/intrinsic types.
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls...
Ja tähän Dart-kielestä esimerkkejä...
Function Overloading - General Dart Discussion | Google Groups
Oct 30, 2011 ... I'm progamming complex object and I am a little dissapointed with some aspect like it haven't got Methods Overloading. I think here could be ...
LabeledDiscussions
Constructor overloading - General Dart Discussion | Google Groups
Feb 12, 2012 ... Subject: Constructor overloading ... Why Dart doesn't allow to overloadconstructors. I have to define ... Subject: Re: Constructor overloading ...
LabeledDiscussions
Operator Overloading: "Convert" Operator - General Dart Discussion ...
Feb 13, 2012 ... I have seen this in other languages as an overloading of the "Convert" operator. Is this possible in Dart? For example: class Material{ ... } ...
LabeledDiscussions
http://www.dartlang.org/articles/idiomatic-dart/
Named constructors
Like most dynamically-typed languages, Dart doesn't support overloading. With methods, this isn't much of a limitation because you can always use a different name, but constructors aren't so lucky. To alleviate that, Dart lets you define named constructors:
class Point {
num x, y;
Point(this.x, this.y);
Point.zero() : x = 0, y = 0;
Point.polar(num theta, num radius) {
x = Math.cos(theta) * radius;
y = Math.sin(theta) * radius;
}
}
Here our Point class has three constructors, a normal one and two named ones. You can use them like so:
var a = new Point(1, 2);
var b = new Point.zero();
var c = new Point.polar(Math.PI, 4.0);
Note that we're still using new here when we invoke the named constructor. It isn't just a static method.
. originally published in etdart.blogspot.fi
Subscribe to:
Posts (Atom)