Tutorial by Examples: collections

A common problem is having a collection of items that all need to meet a certain criteria. In the example below we have collected two items for a diet plan and we want to check that the diet doesn't contain any unhealthy food. // First we create a collection $diet = collect([ ['name' => ...
You can spec collections in a number of ways. coll-of allows you to spec collections and provide some additional constraints. Here's a simple example: (clojure.spec/valid? (clojure.spec/coll-of int?) [1 2 3]) ;; => true (clojure.spec/valid? (clojure.spec/coll-of int?) '(1 2 3)) ;; => tru...
To create a parallel collection from a sequential collection, call the par method. To create a sequential collection from a parallel collection, call the seq method. This example shows how you turn a regular Vector into a ParVector, and then back again: scala> val vect = (1 to 5).toVector vect:...
The macro() function allows you to add new functionality to Illuminate\Support\Collection objects Usage: Collection::macro("macro_name", function ($parameters) { // Your macro }); For example: Collection::macro('uppercase', function () { return $this->map(function ($i...
In an iOS app, your user interface can take on one of a few different general shapes and sizes. These are defined using size classes, which are available through a view or view controller's trait collection. Apple defines two size classes: regular and compact. Each of these size classes are availab...
The Scala Collections framework, according to its authors, is designed to be easy to use, concise, safe, fast, and universal. The framework is made up of Scala traits that are designed to be building blocks for creating collections. For more information on these building blocks, read the official S...
Apply the transformation to non-collection entries, delving into nested collections too and preserving the whole structure. def lst = ['foo', 'bar', ['inner_foo', 'inner_bar']] lst.collectNested { it.toUpperCase() } // [FOO, BAR, [INNER_FOO, INNER_BAR]]
Collections and Maps evaluates to true if not null and not empty and false if null or empty /* an empty map example*/ def userInfo = [:] if (!userInfo) userInfo << ['user': 'Groot', 'species' : 'unknown' ] will add user: 'Groot' , species : 'unknown' as default userInfo since the u...
Geospatial collections generally involve storing GeoJSON in the Mongo database, streaming that data to the client, accessing the browser's window.navigator.geolocation, loading up a Map API, converting GeoJSON to LatLngs, and plotting on the map. Preferably all in realtime. Here are a list of reso...
var a = new List<int> { 1, 2 }; var b = new List<int> { 2, 1 }; Assert.That (a, Is.EqualTo(b)); // fails Assert.That (a, Is.EquivalentTo(b)); // succeeds
List subList(int fromIndex, int toIndex) Here fromIndex is inclusive and toIndex is exclusive. List list = new ArrayList(); List list1 = list.subList(fromIndex,toIndex); If the list doesn't exist in the give range, it throws IndexOutofBoundException. What ever changes made on the list1 wi...
Magento has a powerful set of methods to filter collections. Since there are two types of Objects that can be contained in collections, we must first determine which type of data we are working with before we can filter it. Magento implements a EAV data model for entities such as products and catego...
// get existing collections $orders = Mage::getModel('sales/order')->getCollection(); $products = Mage::getModel('catalog/product')->getCollection(); $customers = Mage::getModel('customer/customer')->getCollection();
PSR-5 proposes a form of Generics-style notation for collections. Generics Syntax Type[] Type<Type> Type<Type[, Type]...> Type<Type[|Type]...> Values in a Collection MAY even be another array and even another Collection. Type<Type<Type>> Type<Type<Type[,...
show collections or show tables or db.getCollectionNames()
Basic printing std::ostream_iterator allows to print contents of an STL container to any output stream without explicit loops. The second argument of std::ostream_iterator constructor sets the delimiter. For example, the following code: std::vector<int> v = {1,2,3,4}; std::copy(v.begin(), v...
The System.Collections.Immutable NuGet package provides immutable collection classes. Creating and adding items var stack = ImmutableStack.Create<int>(); var stack2 = stack.Push(1); // stack is still empty, stack2 contains 1 var stack3 = stack.Push(2); // stack2 still contains only one, st...
Tuples are often used within collections but they must be handled in a specific way. For example, given the following list of tuples: scala> val l = List(1 -> 2, 2 -> 3, 3 -> 4) l: List[(Int, Int)] = List((1,2), (2,3), (3,4)) It may seem natural to add the elements together using im...
Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty. Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, me...
val personMap = Map( 10 -> new Person("Roger", "Moore"), 20 -> new Person("James", "Bond") ) val names = for { (key, person) <- personMap if key > 15 } yield s"$key = ${person.firstName}"

Page 2 of 3