scozv / tango Goto Github PK
View Code? Open in Web Editor NEWWrite some algorithms with JavaScript, Go and other languages, see API docs at https://scozv.github.io/tango
Write some algorithms with JavaScript, Go and other languages, see API docs at https://scozv.github.io/tango
Given a Seq
, we can apply a func
to Map()
or other traversal functions.
Calling Map()
will generate another new instance of Seq
, the time complexity is O(n)
.
Let's say
xs := new Seq() {1, 2, 3}
ys := xs.Map(x => x + 1)
println(ys.Head()) // should be 2
But, if xs
has a size of 1e10, and in some cases, we only need visit few of ys
:
xs := new Seq() {1, 2, 3, ...} // n = 1e10
ys := xs.Map(x => x + 1) // O(n)
println(ys.Head()) // should be 2
In order to implement the Map()
with time only O(1)
:
xs := new Seq() {1, 2, 3, ...} // n = 1e10
ys := xs.Map(x => x + 1) // O(1)
println(ys.Head()) // should be 2
We can try to attach a function on Seq
, instead of traversing the entire list, and apply the function on each element. An idea of this interface will be:
Node {
value Any
next *Node
}
Seq {
head *Node
size int
fn func (int) int // Let's consider `int => int` at this moment
}
By default, fn
is mapping element to itself:
func fn(x int) int = { return x}
When calling Map(nextFn)
, we just need to attach nextFn
to existing fn
:
func (xs *Seq) Map(nextFn) *Seq {
ys := new Seq()
ys.head = xs.head
ys.size = xs.size
ys.fn = xs.fn.withAnotherFunction(nextFn) // Might need a functions queue
}
Instead of replacing the fn
with new nextFn
, we might need a function Queue
to manage the list of functions.
All functions in fn
list will be only invoked when we evaluate the element of Seq
:
func (xs *Seq) Head() int {
xs.fn.ApplyAll(xs.head.value)
}
func (xs *Seq) ToArray() int[] {
ys := make(int[], xs.size)
xs.ForEach (x => {
ys = append(ys, xs.fn.ApplyAll(x.value))
})
return ys
}
Since the Map
doesn't change the size of sequence, we need consider how do we deal with Filter
, Group
which might change the size of the original sequence.
Let's say the priority of Heap
might be identical, we need support peek
group:
heap.peak(): Seq[T]
where for all t
in Seq[T]
, t
has highest priority.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.