GithubHelp home page GithubHelp logo

mesopelagique / iterable Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 22 KB

Iteration classes for object instance, collection and custom object

License: MIT License

Roff 1.69% 4D 98.31%
iterator-pattern 4d-component

iterable's Introduction

Iterable

language language-top code-size release license discord

Iteration classes for string, range, object instance, collection and custom object

Pattern

Iterator pattern

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

In 4D using iterator provided by this component

While ($it.hasNext())
  $aValue:=$it.next()
  // do something
End while 

Usage

You can iterate over different type using the same way

Range

Iterate over a range of numeric

$it:=it .Range.new(1;7)
While ($it.hasNext())
	$value:=$it.next() //1, then 2, ..., until 7

End while 
it .Range.new(1;7).toCollection() // New collection(1;2;3;4;5;6;7)

String

Iterate over string characters, transform it to collection

it .StringIterator.new("Hello World").toCollection() // New collection("H";"e";"l";"l";"o";" ";"W";"o";"r";"l";"d")

eq. of [..."Hello World"] in some languages

Collection & Selection

There is different type of iterator provided, to traverse a collection, an object keys, or values or entries or even custom algorithms.

Type Description
it.CollectionIterator Iterate over a collection
it.SelectionIterator Iterate over a selection
it.ObjectEntryIterator Iterate over an object entries collection
it.ObjectKeyIterator Iterate over an object keys collection
it.ObjectValueIterator Iterate over an object values collection

See some usage in tests

Formula

Type Description
it.IterableIterator Iterate on object which implements two formula: length()->int and getValueAt(index)->value

Go further

4D Tags

You can use pass an iterable to PROCESS 4D TAGS, the template will be easier to read

<!--#4DLOOP ($1.hasNext())-->
<!--#4DEVAL $value:=$1.next()-->
<!--#4DENDLOOP-->

language features?

If a class have a function iterator which return an Iterator instance, we could imagine For each language keyword work with it, instead of keys browsing


mesopelagique

iterable's People

Contributors

e-marchand avatar mesopelagique avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.