This isn't a talk about microservices, NO-SQL, Container solutions or hip new frameworks. This talk will show some of the standard Java APIs that are part of Java since version 5, 6, 7 or 8. All this features are very helpful to create maintainable and future-proof applications, regardless of whether JavaEE, Spring, JavaFX or any other framework is used. The talk will give an overview of some important standard concepts and APIs of Java like annotations, SPI or how to handle null values. Based on an overview of this topics and some samples the talk will answer questions like:
- How can I create my own annotations?
- How can I create a plugin structure without using frameworks like OSGI?
- What's the best way to handle NullPointerExceptions?
âgo-toâ general-purpose sequential collections -from Java To ScalaPhilip Schwarz
Â
The simple idea of this slide deck is that it collects in a single place quite a bit of information that can be used to gain a basic understanding of some key differences between the âgotoâ sequential collections of Java and Scala.
Errata:
* the twitter handle of Daniel Westheide is @kaffeecoder and not @cafeecoder
* on slide 35, the numbers 215, 220, 225 and 230, should be 2^15, 2^20, 2^25 and 2^30 respectively
* on slide 54, the bottom left node should be green rather than black
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...Philip Schwarz
Â
See how feeding FP workhorses map and filter with monadic steroids turns them into the intriguing mapM and filterM.
Graduate to foldM by learning how it behaves with the help of three simple yet instructive examples of its usage.
Use the powers of foldM to generate all permutations of a collection with a simple one-liner.
Exploit what you learned about foldM to solve the N-Queens Combinatorial Problem with an iterative approach rather than a recursive one.
The Java Learning Kit: Chapter 6 â Arrays
Copyright 2015 by C. Herbert, all rights reserved.
Last edited January, 2015 by C. Herbert
This document is a chapter from a draft of the Java Learning Kit, written by Charles Herbert, with editorial input from Craig
Nelson, Christopher Quinones, Matthew Staley, and Daphne Herbert. It is available free of charge for students in Computer
Science courses at Community College of Philadelphia during the Spring 2015 semester.
This material is protected by United States and international copyright law and may not be reproduced, distributed,
transmitted, displayed, published or broadcast without the prior written permission of the copyright holder. You may not alter
or remove any trademark, copyright or other notice from copies of the material.
The Java Learning Kit:
Chapter 6
Arrays
Lesson 6.1 â Arrays
Lesson 6.2 â One Dimensional Arrays in Java
Lesson 6.3 â Operations on Numeric Arrays
Lesson 6.4 â Searching and Sorting Arrays
Lesson 6.5 â Text File Input and Output in Java
Lab 6 â Programming Example: Moving Data between Arrays and Files
JLK Chapter 6 â Arrays DRAFT 2015 Edition pg. 2
Contents
Chapter 6 Learning Outcomes .................................................................................................................... 3
Arrays .............................................................................................................................. 4
Array Storage in Computer Memory ................................................................................................... 5
CheckPoint 6.1 .................................................................................................................................... 6
One Dimensional Arrays in Java ...................................................................................... 7
What is actually in an array in Java? ................................................................................................... 7
Assigning Values to Array Elements .................................................................................................. 10
The Array length Property; Iterating an Array ................................................................................... 11
The Enhanced for Statement ............................................................................................................ 12
Copying an Array ............................................................................................................................... 13
Arrays as Method Parameters in Java ............................................................................................... 13
Passing an Array Element as a Method Parameter ........................................................................... 15
CheckPoint 6.2 ..............................................................................................................
A book for learning puppet by real example and by building code. Second chapters takes you through all basics of Puppet and enough ruby to work with Puppet.
This isn't a talk about microservices, NO-SQL, Container solutions or hip new frameworks. This talk will show some of the standard Java APIs that are part of Java since version 5, 6, 7 or 8. All this features are very helpful to create maintainable and future-proof applications, regardless of whether JavaEE, Spring, JavaFX or any other framework is used. The talk will give an overview of some important standard concepts and APIs of Java like annotations, SPI or how to handle null values. Based on an overview of this topics and some samples the talk will answer questions like:
- How can I create my own annotations?
- How can I create a plugin structure without using frameworks like OSGI?
- What's the best way to handle NullPointerExceptions?
âgo-toâ general-purpose sequential collections -from Java To ScalaPhilip Schwarz
Â
The simple idea of this slide deck is that it collects in a single place quite a bit of information that can be used to gain a basic understanding of some key differences between the âgotoâ sequential collections of Java and Scala.
Errata:
* the twitter handle of Daniel Westheide is @kaffeecoder and not @cafeecoder
* on slide 35, the numbers 215, 220, 225 and 230, should be 2^15, 2^20, 2^25 and 2^30 respectively
* on slide 54, the bottom left node should be green rather than black
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...Philip Schwarz
Â
See how feeding FP workhorses map and filter with monadic steroids turns them into the intriguing mapM and filterM.
Graduate to foldM by learning how it behaves with the help of three simple yet instructive examples of its usage.
Use the powers of foldM to generate all permutations of a collection with a simple one-liner.
Exploit what you learned about foldM to solve the N-Queens Combinatorial Problem with an iterative approach rather than a recursive one.
The Java Learning Kit: Chapter 6 â Arrays
Copyright 2015 by C. Herbert, all rights reserved.
Last edited January, 2015 by C. Herbert
This document is a chapter from a draft of the Java Learning Kit, written by Charles Herbert, with editorial input from Craig
Nelson, Christopher Quinones, Matthew Staley, and Daphne Herbert. It is available free of charge for students in Computer
Science courses at Community College of Philadelphia during the Spring 2015 semester.
This material is protected by United States and international copyright law and may not be reproduced, distributed,
transmitted, displayed, published or broadcast without the prior written permission of the copyright holder. You may not alter
or remove any trademark, copyright or other notice from copies of the material.
The Java Learning Kit:
Chapter 6
Arrays
Lesson 6.1 â Arrays
Lesson 6.2 â One Dimensional Arrays in Java
Lesson 6.3 â Operations on Numeric Arrays
Lesson 6.4 â Searching and Sorting Arrays
Lesson 6.5 â Text File Input and Output in Java
Lab 6 â Programming Example: Moving Data between Arrays and Files
JLK Chapter 6 â Arrays DRAFT 2015 Edition pg. 2
Contents
Chapter 6 Learning Outcomes .................................................................................................................... 3
Arrays .............................................................................................................................. 4
Array Storage in Computer Memory ................................................................................................... 5
CheckPoint 6.1 .................................................................................................................................... 6
One Dimensional Arrays in Java ...................................................................................... 7
What is actually in an array in Java? ................................................................................................... 7
Assigning Values to Array Elements .................................................................................................. 10
The Array length Property; Iterating an Array ................................................................................... 11
The Enhanced for Statement ............................................................................................................ 12
Copying an Array ............................................................................................................................... 13
Arrays as Method Parameters in Java ............................................................................................... 13
Passing an Array Element as a Method Parameter ........................................................................... 15
CheckPoint 6.2 ..............................................................................................................
A book for learning puppet by real example and by building code. Second chapters takes you through all basics of Puppet and enough ruby to work with Puppet.
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
Â
Scala gives you awesome expressive power, but how to make best use of it? In my talk I will discuss the question what makes good Scala style. We will start with syntax and continue with how to name things, how to mix objects and functions, where (and where not) to use mutable state, and when to use which design pattern. As most questions of style, the discussion will be quite subjective, and some of it might be controversial. I am looking forward to discuss these topics with the conference attendees.
I Heard React Was Good
with Ryan Christiani
There is no doubt React is here to stay, itâs popularity is on the rise. But if you are new to JS or new to JS frameworks you might be wondering WHY is it so popular. In this talk we will look at why we have React and what problems it solves. The concepts of reusable components, their life cycles and the common terminology. We will also explore what the workflow for building a modern React app looks like, and where React can fall short. Learning React in 2016 will be an important step in your career, so lets dive in!
OBJECTIVE
Break down the concepts behind React and make it easy for people to start building with it.
TARGET AUDIENCE
JavaScript developers or project mangers looking to get a better understanding of React.
ASSUMED AUDIENCE KNOWLEDGE
Intermediate understanding of JavaScript.
FIVE THINGS AUDIENCE MEMBERS WILL LEARN
Why do we have react, what problems does it solve
React components
Component life cycles
Common terminology
Where React falls short
Lab 1 Recursion Introduction  Tracery (tracery.io.docxsmile790243
Â
Lab 1: RecursionÂ
Introduction Â
Tracery (tracery.io) is a simple text-expansion language made by one of your TAs as a
homework assignment for one of Prof. Mateas's previous courses. It now has tens of
thousands of users, and runs about 7000 chatbots on Twitter (you never know where a
homework will take you!).
Tracery uses context-free grammars
( âhttps://en.wikipedia.org/wiki/Context-free_grammarâ) to store information about how to
expand rules. A Tracery grammar is a set of keys, each of which has some set of
expansions that can replace it. In our version, the line
"beverage:tea|coffee|cola|milk"â means that the âsymbol â â"beverage"â can be
replaced with any of those four options. You âreplaceâ a symbol whenever you see it in
hashtags. This rule â"#name# drank a glass of #beverage#"â will have the â"name"
and â"beverage"â symbols replaced with expansions associated with those symbols. In
the case of the â"beverage"â rule above, which has four possible expansions, one will be
picked at random. If the replacement rule âalso â has hashtags in it, we replace those, and
if those replacements have hashtags.... we keep replacing things until all the hashtags
are gone, ârecursivelyâ.
In this assignment, you will be implementing a simplified version of Tracery in Java, and
then using it to generate generative text. You will also be writing your own grammar to
generate new texts (hipster cocktails, emoji stories, or nonsense poems).
OutlineÂ
â Compile and run a Java program
â Save all the arguments
â Load the Tracery files
â Output all the rules
â Expand rules and print them to the screen
Compile and run a Java programÂ
This program has several files (âRule.javaâ, and âTraceryRecursion.javaâ). We can't
run â these files as code, as we would with other âinterpretedâ languages (like Javascript
https://en.wikipedia.org/wiki/Context-free_grammar
or Python). For Java, we need the computer to put them all together and translate it to
machine code, in a process called âcompilingâ.
You will compile and run your Java program from the command line. When you see â$â,
this means that it is a command that you will enter in the âcommand lineâ. Windows and
Unix (such as Linux or the Mac terminal) command lines are a little different, be sure
you know the basics of navigating the one you are using.
Do you have Java installed on your machine? What version? Let's find out! Type the
following into your command line: â$ javac -version $ java -versionâ You should
have at least Javac 1.8 and Java 1.8 (often called âJava 8â) installed. If that's not the
case, this is a good time to fix that by updating your Java. We will be using some
features of Java 8 in this class.
Compile your java program.
$ javac TraceryRecursion.java
So far, it will compile without errors.
Look in the folder, and you will see that you have a new file âTraceryRecursion.classâ.
This is the compiled version of your file. You can now ârun ...
I gave this talk at Squares Conference 2016.
http://squaresconference.com
Code demo from the talkâŚ
http://t7.github.io/react-starter
https://github.com/t7/react-starter
Presented at FITC Toronto 2016
See details at www.fitc.ca
There is no doubt React is here to stay, itâs popularity is on the rise. But if you are new to JS or new to JS frameworks you might be wondering WHY is it so popular. In this talk we will look at why we have React and what problems it solves. The concept of reusable components, their life cycles and the common terminology. We will also explore what the workflow for building a modern React app looks like, and where React can fall short. Learning React in 2016 will be an important step in your career, so lets dive in!
Objective
Break down the concepts behind React and make it easy for people to start building with it.
Target Audience
JavaScript developers or project mangers looking to get a better understanding of React.
Assumed Audience Knowledge
Intermediate understanding of JavaScript.
Five Things Audience Members Will Learn
Why do we have react, what problems does it solve
React components
Component life cycles
Common terminology
Where React falls short
ECMAScript is the name of the international standard that defines JavaScript. ES6 â ECMAScript 2015. Latest ECMAScript version is ES7 which is ECMAScript 2016.
Basically it is a superset of es5
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
Â
Scala gives you awesome expressive power, but how to make best use of it? In my talk I will discuss the question what makes good Scala style. We will start with syntax and continue with how to name things, how to mix objects and functions, where (and where not) to use mutable state, and when to use which design pattern. As most questions of style, the discussion will be quite subjective, and some of it might be controversial. I am looking forward to discuss these topics with the conference attendees.
I Heard React Was Good
with Ryan Christiani
There is no doubt React is here to stay, itâs popularity is on the rise. But if you are new to JS or new to JS frameworks you might be wondering WHY is it so popular. In this talk we will look at why we have React and what problems it solves. The concepts of reusable components, their life cycles and the common terminology. We will also explore what the workflow for building a modern React app looks like, and where React can fall short. Learning React in 2016 will be an important step in your career, so lets dive in!
OBJECTIVE
Break down the concepts behind React and make it easy for people to start building with it.
TARGET AUDIENCE
JavaScript developers or project mangers looking to get a better understanding of React.
ASSUMED AUDIENCE KNOWLEDGE
Intermediate understanding of JavaScript.
FIVE THINGS AUDIENCE MEMBERS WILL LEARN
Why do we have react, what problems does it solve
React components
Component life cycles
Common terminology
Where React falls short
Lab 1 Recursion Introduction  Tracery (tracery.io.docxsmile790243
Â
Lab 1: RecursionÂ
Introduction Â
Tracery (tracery.io) is a simple text-expansion language made by one of your TAs as a
homework assignment for one of Prof. Mateas's previous courses. It now has tens of
thousands of users, and runs about 7000 chatbots on Twitter (you never know where a
homework will take you!).
Tracery uses context-free grammars
( âhttps://en.wikipedia.org/wiki/Context-free_grammarâ) to store information about how to
expand rules. A Tracery grammar is a set of keys, each of which has some set of
expansions that can replace it. In our version, the line
"beverage:tea|coffee|cola|milk"â means that the âsymbol â â"beverage"â can be
replaced with any of those four options. You âreplaceâ a symbol whenever you see it in
hashtags. This rule â"#name# drank a glass of #beverage#"â will have the â"name"
and â"beverage"â symbols replaced with expansions associated with those symbols. In
the case of the â"beverage"â rule above, which has four possible expansions, one will be
picked at random. If the replacement rule âalso â has hashtags in it, we replace those, and
if those replacements have hashtags.... we keep replacing things until all the hashtags
are gone, ârecursivelyâ.
In this assignment, you will be implementing a simplified version of Tracery in Java, and
then using it to generate generative text. You will also be writing your own grammar to
generate new texts (hipster cocktails, emoji stories, or nonsense poems).
OutlineÂ
â Compile and run a Java program
â Save all the arguments
â Load the Tracery files
â Output all the rules
â Expand rules and print them to the screen
Compile and run a Java programÂ
This program has several files (âRule.javaâ, and âTraceryRecursion.javaâ). We can't
run â these files as code, as we would with other âinterpretedâ languages (like Javascript
https://en.wikipedia.org/wiki/Context-free_grammar
or Python). For Java, we need the computer to put them all together and translate it to
machine code, in a process called âcompilingâ.
You will compile and run your Java program from the command line. When you see â$â,
this means that it is a command that you will enter in the âcommand lineâ. Windows and
Unix (such as Linux or the Mac terminal) command lines are a little different, be sure
you know the basics of navigating the one you are using.
Do you have Java installed on your machine? What version? Let's find out! Type the
following into your command line: â$ javac -version $ java -versionâ You should
have at least Javac 1.8 and Java 1.8 (often called âJava 8â) installed. If that's not the
case, this is a good time to fix that by updating your Java. We will be using some
features of Java 8 in this class.
Compile your java program.
$ javac TraceryRecursion.java
So far, it will compile without errors.
Look in the folder, and you will see that you have a new file âTraceryRecursion.classâ.
This is the compiled version of your file. You can now ârun ...
I gave this talk at Squares Conference 2016.
http://squaresconference.com
Code demo from the talkâŚ
http://t7.github.io/react-starter
https://github.com/t7/react-starter
Presented at FITC Toronto 2016
See details at www.fitc.ca
There is no doubt React is here to stay, itâs popularity is on the rise. But if you are new to JS or new to JS frameworks you might be wondering WHY is it so popular. In this talk we will look at why we have React and what problems it solves. The concept of reusable components, their life cycles and the common terminology. We will also explore what the workflow for building a modern React app looks like, and where React can fall short. Learning React in 2016 will be an important step in your career, so lets dive in!
Objective
Break down the concepts behind React and make it easy for people to start building with it.
Target Audience
JavaScript developers or project mangers looking to get a better understanding of React.
Assumed Audience Knowledge
Intermediate understanding of JavaScript.
Five Things Audience Members Will Learn
Why do we have react, what problems does it solve
React components
Component life cycles
Common terminology
Where React falls short
ECMAScript is the name of the international standard that defines JavaScript. ES6 â ECMAScript 2015. Latest ECMAScript version is ES7 which is ECMAScript 2016.
Basically it is a superset of es5
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
Â
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Â
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
Â
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. Whatâs changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
JMeter webinar - integration with InfluxDB and GrafanaRTTS
Â
Watch this recorded webinar about real-time monitoring of application performance. See how to integrate Apache JMeter, the open-source leader in performance testing, with InfluxDB, the open-source time-series database, and Grafana, the open-source analytics and visualization application.
In this webinar, we will review the benefits of leveraging InfluxDB and Grafana when executing load tests and demonstrate how these tools are used to visualize performance metrics.
Length: 30 minutes
Session Overviewâ
-------------------------------------------
During this webinar, we will cover the following topics while demonstrating the integrations of JMeter, InfluxDB and Grafana:
- What out-of-the-box solutions are available for real-time monitoring JMeter tests?
- What are the benefits of integrating InfluxDB and Grafana into the load testing stack?
- Which features are provided by Grafana?
- Demonstration of InfluxDB and Grafana using a practice web application
To view the webinar recording, go to:
https://www.rttsweb.com/jmeter-integration-webinar
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Â
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
State of ICS and IoT Cyber Threat Landscape Report 2024 previewPrayukth K V
Â
The IoT and OT threat landscape report has been prepared by the Threat Research Team at Sectrio using data from Sectrio, cyber threat intelligence farming facilities spread across over 85 cities around the world. In addition, Sectrio also runs AI-based advanced threat and payload engagement facilities that serve as sinks to attract and engage sophisticated threat actors, and newer malware including new variants and latent threats that are at an earlier stage of development.
The latest edition of the OT/ICS and IoT security Threat Landscape Report 2024 also covers:
State of global ICS asset and network exposure
Sectoral targets and attacks as well as the cost of ransom
Global APT activity, AI usage, actor and tactic profiles, and implications
Rise in volumes of AI-powered cyberattacks
Major cyber events in 2024
Malware and malicious payload trends
Cyberattack types and targets
Vulnerability exploit attempts on CVEs
Attacks on counties â USA
Expansion of bot farms â how, where, and why
In-depth analysis of the cyber threat landscape across North America, South America, Europe, APAC, and the Middle East
Why are attacks on smart factories rising?
Cyber risk predictions
Axis of attacks â Europe
Systemic attacks in the Middle East
Download the full report from here:
https://sectrio.com/resources/ot-threat-landscape-reports/sectrio-releases-ot-ics-and-iot-security-threat-landscape-report-2024/
Dev Dives: Train smarter, not harder â active learning and UiPath LLMs for do...UiPathCommunity
Â
đĽ Speed, accuracy, and scaling â discover the superpowers of GenAI in action with UiPath Document Understanding and Communications Miningâ˘:
See how to accelerate model training and optimize model performance with active learning
Learn about the latest enhancements to out-of-the-box document processing â with little to no training required
Get an exclusive demo of the new family of UiPath LLMs â GenAI models specialized for processing different types of documents and messages
This is a hands-on session specifically designed for automation developers and AI enthusiasts seeking to enhance their knowledge in leveraging the latest intelligent document processing capabilities offered by UiPath.
Speakers:
đ¨âđŤ Andras Palfi, Senior Product Manager, UiPath
đŠâđŤ Lenka Dulovicova, Product Program Manager, UiPath
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
Â
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
DevOps and Testing slides at DASA ConnectKari Kakkonen
Â
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
Â
In today's fast-changing business world, Companies that adapt and embrace new ideas often need help to keep up with the competition. However, fostering a culture of innovation takes much work. It takes vision, leadership and willingness to take risks in the right proportion. Sachin Dev Duggal, co-founder of Builder.ai, has perfected the art of this balance, creating a company culture where creativity and growth are nurtured at each stage.
2. Laravel Collections Unraveled
Jeff Madsen
This book is for sale at http://leanpub.com/laravelcollectionsunraveled
This version was published on 2017-11-04
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Š 2015 - 2017 Jeff Madsen
4. Why this âePamphletâ?
âLaravel Collections are fantastic!â I constantly read this tweeted - and couldnât agree more. More
than five dozen useful functions that you might otherwise have to write out yourself - five dozen
tested functions, important to say - all chain-able, documented and ready to make your life easier.
But why write an eBook about it? (I jokingly call it an âePamphletâ because of its length, but I
suppose eBook is the proper term).
Remember the early days of learning your first programming language? You spent so much time
coming up with an algorithm to solve your problem - and then the instructor would put his
solution up on the overhead (are you old enough to remember âoverheadsâ?) complete with Built-
In-Language-Function-That-Solves-This-Problem()? How you cried, âUnfair! How am I supposed to
know that function even exists?â
Well, you know it exists by studying the source code. Thatâs what we are going to do. In the
process, weâre going to perhaps improve our basic PHP skills a bit by getting more familiar with
ArrayObjects, CachingIterators, IteratorAggregates and some other handy tools. Weâre going to not
just read through the list of available methods, but come up with some practical examples of when
they might be useful to us.
All that seemed too long to write a blog post about, and I find that serial tutorials never really
sink in with me personally. Hopefully this will prove useful enough that youâll keep it handy as a
reference item while working and internalizing the contents. As an eBook I can continually improve
the examples and let you know when better content is available, or when a new version of Laravel
introduces more functionality.
SoâŚletâs jump in!
5. What are collections?
Just what are Laravel Collections, after all? Theyâre sort of like Arrays, in that you can add, remove
and iterate over the contents. The documentation calls them âconvenient wrappersâ. They have all
these extra functions - and you can chain them!
In fact, looking at the IlluminateSupportCollection class itself gives us the answer - âclass
Collection implements ArrayAccessâ. An ArrayAccess1
is an interface used primarily to define an
ArrayObject2
. An ArrayObject is the actual class you build and work with. The best definition and
examples of these two terms I could find comes from the inestimable Lorna Jane Mitchellâs article
ArrayAccess vs ArrayObject3
. Please give it a quick read - it is short, does a great job, and Iâm
going to reference her second example later in this chapter. So - if you havenât figured it out yet - a
Collection is a custom ArrayObject, implementing ArrayAccess and some other interfaces in much
the same way.
Letâs look at those and have a quick review of what they do so we can better understand the
functions themselves when we get to them. If you havenât already, open class Collection Illumi-
nateSupportCollection. Youâll see it implements: ArrayAccess, Countable, IteratorAggregate,
JsonSerializable, Arrayable, and Jsonable.
ArrayAccess - this is what makes a class behave like an array. It forces methods offsetExists(),
offsetGet(), offsetSet(), offsetUnset(). Youâll see those four methods near the bottom of the
class file. As a matter of fact, youâll see the exact same thing in the PrettyBasket class from Lorna
Jane Mitchellâs article - which is why I recommended you read it.
Countable4
- that other well-known function, count(), comes from here. In PHP, arrays donât actually
implement this interface - thatâs why you donât see $myArray->count(). Our ArrayObject, however,
has included it.
IteratorAggregate5
- has the function getIterator() and is what lets us iterate over private
properties of our objects. For example, in the manualâs myData class example, if you removed the
IteratorAggregate implementation you would still be able to foreach over the properties. However,
if you made them private you would not. Implementing this interface gives us that access.
Finally, we have JsonSerializable and Laravelâs own Arrayable and Jsonable. These interfaceâs allow
us to switch back and forth between json and array style output.
So what do we have at this point? Our own custom ArrayObject class that allows array-like access
to items (corresponding to elements in an actual array), can add and remove items, can count how
1
http://php.net/manual/en/class.arrayaccess.php
2
http://php.net/manual/en/class.arrayobject.php
3
http://www.lornajane.net/posts/2011/arrayaccess-vs-arrayobject
4
http://php.net/manual/en/class.countable.php
5
http://php.net/manual/en/class.iteratoraggregate.php
6. What are collections? 3
many items it holds, can traverse those items, and can return those items in array or json format.
Notice, also, that these items are stored and operated on internally as an array.
One thing we do NOT have, however - is a resultset. People often comment on my Collections tips,
asking âwhy not just use a WHERE clauseâ or other sql solutions. That might be a better solution
under certain circumstances, but a Collection is not just a fancy wrapper for a database resultset.
You may not even have access to the actual sql, such as when parsing an expensive API response,
or you may wish to use these functions for your own internal data processing. So these methods
should be looked at in a wider context.
Now we are ready to start looking at the class functions.
7. Array Mimicking
Letâs get into the meat of the subject with the actual functions of the Collection object. Iâm going to
break them in several arbitrary groups that I think will help us remember what they do âŚ
These first few you might almost think of as âmagic functionâ. They are part of the actual
construction of the object and not something most people even think about. Do you remember
our ArrayAccess we are implementing as a base for this object? This is what makes it all meet the
requirements of that interface, and allow it to all work like an array.
It is unlikely youâll ever use these functions directly, unless you wish to inherit from the Collection
class for some reason.
__construct($items = [])
offsetExists($key)
offsetGet($key)
offsetSet($key, $value)
offsetUnset($key)
These functions work just like their native php counterparts, but as class functions. This means
$users->offsetExists(3)
gives the same result as
array_key_exists(3, $users)
Function offsetSet($key, $value) can work two ways:
$collection->offsetSet(3, 200);
will replace the key value at position 3;
$collection->offsetSet(null, 200);
will act just like
$collection[] = 200;
and add a new element on the end.
One last âdeep diveâ into this area - the __construct. Notice that the __construct likes to start with
an array. If you look in the source code, however, youâll see that if it doesnât get an array, it will call
getArrayableItems($items). This will cast Arrayables, Json objects, or even other Collections as
an array. Furthermore, anything that can be cast as an array will be - but not always with the result
you were thinking:
9. Array Mechanics
Used to format, transform or otherwise manipulate the structure of the $items array. Iâm putting
caching in here, as well.
toArray() - will transform from a Collection back to a straight array.
toJson($options = 0) - transforms the Collection to a json encoded string.
jsonSerialize() - a wrapper around toArray(). Making it serializeable. Thatâs it.
We wonât linger for long over these functions, but I thought it was a good opportunty to point
out something youâll notice a lot in the source and might wonder about. Have a look at the actual
function toJson($options = 0):
public function toJson($options = 0) { return json_encode($this->toArray(), $options);
}
Itâs true that this function actual handles two things, first converting to an array and then applying
the php function json_encode(). A few of the functions donât even do that much - they are simple
wrappers around php core functions. Why do that?
Chaining! Laravel is not just about RAD, it is about working in a clean, easy to read and work with
mannerâŚâeloquentâ, if you will. Youâll notice a very heavy influence from Ruby on Rails when you
work with it for a while. Php loves to wrap functions inside of function inside of functions. Laravel
would prefer this:
$collection= collect([11,12,13,14,15])->shuffle()->toJson();
You can read it, left to right, like a book. Nice, donât you agree?
Looking at our iterator functions, we have
getIterator() - Converts the Collection to an ArrayIterator
getCachingIterator($flags = CachingIterator::CALL_TOSTRING) - Converts the Collection to a
CachingIterator built around an ArrayIterator
Letâs take a look the following little code snippet to understand this better, and see an interesting
use.
$collection= collect([11,12,13,14,15])->getCachingIterator();
This gives as a CachingIterator, which internally holds an ArrayIterator with the items 11,12,13,14,15.
Our CachingIterator is empty, but our ArrayIterator is set to the first element. Therefore,
dump($collection->current()); //yields null
dump($collection->getInnerIterator()->current()); //yields 11, our first element
10. Array Mechanics 7
You see that because the outer CachingIterator does not cache anything until it has been âcalledâ by
the internal iterator, it is always one step behind the ArrayIterator. This lets us âlook aheadâ in our
ArrayIterator to see what is coming next.
Advance one item and see for yourself:
$collection->next();
dump($collection->current()); // yields 11
dump($collection->getInnerIterator()->current()); // yields 12
Neat!
That takes us deep into the internals of the Collection class. Letâs move toward more familiar ground
now with a look at the âWrapperâ Methods.
11. Array Wrappers
Iâve nicknamed this group the âwrappersâ because by and large, they imitate the similarly named
functions that exist in the PHP language itself. All of these work internally with the $items array
that is created when you instantiate the Collection, meaning that they can be strung together in a
more readable fashion as collect([])->someMethod()->someOtherMethod().
Another thing to note with all of the functions that work with finding or extracting certain keys is,
they all tend to funnel through an object called IlluminateSupportArr, using its get() method.
If you look at this method, youâll see that this is what lets us use dot notation to access our multi-
dimensional array keys in ways you are already familiar:
$api_key= Config::get('services.mandrill.secret');
This is one of the reasons I enjoy working with Collections; after digging through it for a while and
following the different Traits and Interfaces it uses, you start to see how so much of the Core ties
together. If you spend enough time youâll see this thread run through the Request object, Eloquent
and virtually everywhere else. What seems at first to be a giant codebase slowly condenses down to
something much more easy to get your head around.
Letâs jump into the methods. Because these are âwrapperâ functions, there is a long list of them
that I really donât have many Laravel-specific comments to make. The goal here is simply to let you
recognize that the functions exists on this class; later we will show some useful examples with many
of them. However, there are a few that work a little bit differently than their PHP counterparts that
are interesting to take a look at. For example:
implode($value, $glue = null)
This can work in one of two ways. With normal array contents, the first param is the glue and
everything functions as you would expect. But look what happens when you feed it an array of
arrays or objects:
1 $collection = collect([
2
3 ['field1'=> 11],
4 ['field2'=> 12],
5 ['field1'=> 13],
6 ['field2'=> 14],
7 ['field1'=> 15]
8
9 ])->implode('field1');
10
12. Array Wrappers 9
11 dump($collection);
12
13 // Result: "111315"
The items are first filtered and then glued! You can do the same with your database results:
1 $users = AppUser:: all();
2
3 $collection = collect($users)->implode('first_name', '-');
4
5 // Results: "Jeff-Joe-Gerry"
This simple tool means you can make one call to your data, then cleanly and easily rework it into
different presentations. That may not be a big deal if you have full control over your datasource, but
could be a Godsend if you need to make an expensive call to a restrictive api source.
Likewise, these two functions are ones you might prefer to do in the query when you can, but when
you canât, their callback ability can be wonderful. Look at these three ways to use sum():
1 echo collect([1,3,5])->sum(); // Result: 9
2
3 echo collect([
4
5 ['field1'=> 11],
6 ['field2'=> 12],
7 ['field1'=> 13],
8 ['field2'=> 14],
9 ['field1'=> 15]
10
11 ])->sum('field1'); // Result: 39
12
13 echo collect([
14
15 ['field1'=> 11],
16 ['field2'=> 12],
17 ['field1'=> 13],
18 ['field2'=> 14],
19 ['field1'=> 15]
20
21 ])->sum(function($field) {
22
23 if (isset($field['field2'])) return $field['field2'];
24
25 }); // Result: 26
13. Array Wrappers 10
max(), min() and avg() all work the same way. Let me tell you how much I wish I had these back
when I was creating sales and marketing reports! Try this yourself with the search() function, as
well.
The rest of this list holds no big surprises that Iâve noticed - they are generally just the array_*
version of the native function, written in a clean, chainable fashion to use the internal $items. Iâll
include them for completeness sake; if you notice anything unusual, let me know and Iâll add it in.
flip() - array_flip()
keys() - array_keys()
pop() - array_pop()
pull($key, $default = null) - Arr::pull()
prepend($value) - array_unshift()
reduce(callable $callback, $initial = null) - array_reduce()
reverse() - array_reverse()
shift() - array_shift()
diff($items) - array_diff()
slice($offset, $length = null, $preserveKeys = false) - array_slice()
chunk($size, $preserveKeys = false) - array_chunk()
splice($offset, $length = null, $replacement = []) - array_splice()
map(callable $callback) - array_map()
merge($items) - array_merge()
intersect($items) - array_intersect()
values() - array_values()
count() - count()
push($value) - uses $this->offsetSet(), but key is set to null so always adds to end of
the Collection
put($key, $value) - uses $this->offsetSet() , but key is passed so you can update or add
to a particular place in the collection
14. Ways to sort
This is a short chapter, but I think it is good to pull these into their own group and investigate them
a little bit. Most of this information is already in the documentation and well-explained, but itâs a
good opportunity to play with a few more examples to see what creative solutions we can come up
with.
shuffle()
Shuffle just randomizes your items. It will NOT maintain your keys, but unlike the PHP function, it
returns a Collection rather than a boolean, which never made any sense anyway.
sort(callable $callback = null)
sortBy($callback, $options = SORT_REGULAR, $descending = false)
sortByDesc($callback, $options = SORT_REGULAR)
To summarize, the differences we have among the three functions is that sort() works with a
simple array object to sort on the first-level values, either in the default alphanumerical manner or
according to the callback you pass in. A very small note about something that is incorrect in the
current documentation - the internal sort uses uasort(), not usort, and so the keys are maintained.
This is why in the example they show the results with $collection->values()->all(), which is
all you need to do if you want a new array of the values only.
If you need to work with a subset of the $items, i.e., a field on the resultset, then you want to go
with sortBy() and tell it which field to pluck and sort on. Optionally, instead of the field if you pass
it a callable (thatâs to say, the name of a function to use as a callback, or an anonymous function
itself) it will work with your own sorting algorithm.
sortByDesc() is merely sortBy() with the third param set to âtrueâ, for convenience when you
donât wish to use the second $options argument.
Thereâs only so much you can demonstrate with sorting all by itself, so let me leave you with one
interesting sort and the promise that weâll visit these again combined with other functions.
Marketing is infamous for wanting custom sorts and displays. Imagine this array was full of item
data and they wanted it to show up in a particular order on the page, say Bookcase, Desk, ChairâŚ
15. Ways to sort 12
1 $collection = collect([
2
3 ['name' => 'Desk'],
4 ['name' => 'Chair'],
5 ['name' => 'Bookcase'],
6
7 ]);
We could use the callback to sort it exactly as they wanted:
1 $sorted = $collection->sortBy(function ($product, $key)
2 {
3
4 return array_search($product['name'], [1=>'Bookcase', 2=>'Desk', 3=>'Chair']
5 );
6
7 });
8
9 dump($sorted->values()->all());
Hopefully that tip was worth the price of admission. On to filtersâŚ
16. Ways to filter
We saw last chapter how we could sort our data, which is handy, but where the real fun begins
is when we start filtering the collection items. Most of these methods have certain similarities, the
most outstanding being the ability to use a callback function to further modify its behavior. Letâs
group and compare them as weâve been doing.
The first group weâll look at is what I call the âvalidationâ filters. Not really filters in the traditional
sense, but rather confirm the existence of certain data.
isEmpty()
isEmpty() is a little bit interesting not so much for the function, which is just a wrapper around
empty($items), but for how it is and can be used. First, compare the following (using a User id that
doesnât exist):
$user1 = AppUser:: find(100);
dump($user1->isEmpty());
with
$user2 = AppUser:: find([100]);
dump($user2->isEmpty());
The first one should error out on you - the result of $user1 is not a collection. In the second one I
used a little trick to force the result to be a collection so we could see it work properly. This is one of
the small âgotchaâsâ developers often come across when they first decide to start using the isEmpty()
function. Eloquent will, by default, not return a single row result type as a collection (as of v5.1 -
this is being discussed and always returning a collection is being considered, so watch this space).
Why not just always use empty() and be safe? No reason, really. Itâs not âbetterâ and doesnât have any
obscure, under the hood differences. As a function of an object, you can occasionally do interesting
things like a hand-rolled contains() function:
$users = AppUser::all();
dump($users->where('first_name', 'Jeff')->isEmpty());
But this is really just flexibility; thereâs no particular advantage I can think of for doing so in ordinary
work.
has($key)
has() uses the internal offsetExists to determine if the key is there; it does not check it for
emptiness. In other words, if the key type is set to null, or 0, it will still pass this method as true.
17. Ways to filter 14
Be careful! This is actually very different from the Request::has() function, which does check for
emptiness and can even check for multiple keysâ existence at once.
contains($key, $value = null)
contains looks for values, or key-value pairs. Again, though, it is only returning a boolean about
whether or not the value is found, not how many or the records themselves. âIs there a user with
first name of Fred?â This will tell us. You may also get more precise in your searches by creating a
callback for it to use.
That group is useful for checking for the existence of keys or values, but what about actually pulling
them out and using them? Have no fear - this is one of the areas that Laravel Collections really
shine!
get($key, $default = null)
get() is the matching pair to has(), and also allows for a default value if not found (like youâve
seen on the Request object).
If you need a full set of data, try:
pluck($value, $key = null)
This is the equivalent method for contains(), in that you tell it the field of the multi-array you
want and it will return all of those items. This works for a collection of models, of course, so:
$users = AppUser::all();
dump($users->pluck('last_name'));
Will give you a directory of last names. Signify the key youâd like with the second parameter:
$users = AppUser::all();
dump($users->pluck('last_name', 'id'));
Because one of the most common tasks we have for working with result sets is to create dropdowns,
we also have a specialized form of pluck() called lists($value, $key = null). (NOTE: lists has
been deprecated as of 5.2. It is still available in 5.3, but expect it to disappear soon.) There is absolutely
no difference - lists() merely calls pluck() internally. If anything, the reason this second function
was created is just to spotlight it for you so you make a habit of using this syntax rather than using
a foreach loop to specially build your dropdown data.
While pluck() is nice for grabbing all the last names, sometimes you want the complete records but
filtered on a certain name. In this case, try where() or filter.
â $users = AppUser::all();
dump($users->where(âfirst_nameâ, âJeffâ)); â
is the same as:
dump( $users->filter(function($item){ return ($item['first_name'] == 'Jeff'); }) );
18. Ways to filter 15
As you can see, filter offers us much more flexibility, whereas where gives us simplicity to
remember and use. Because these are Collections, we can chain them together, such as:
$users = AppUser::all(); dump($users->where('first_name', 'Jeff')->where('active',1));
Be careful! This looks very much like the Eloquent where() function - until you start trying
things like where('last_name', '!=', 'Madsen') or whereActive(1). You quickly realize this is
an array_filter() function and youâll need to write filter callbacks for that.
Speaking of filter callbacks - you may already know that array_filter($array); with no callback
will simply filter out empty elements, but the Collection classâ filter will do the same:
dump( collect([null, 2, 0, 6])->filter() ); // Results: array(2, 6)
Be careful, though - notice that both the null and the possibly valid 0 elements are removed.
Letâs end with a few lesser known functions that might be âjust the thingâ some day.
reject($callback) is simply a filter() that returns a set of excluded values. Handy tool for finding
outliers for marketing reports, or suspicious user agents.
unique($key = null) does what it says for simple arrays, but careful with more complex objects.
On a multi-dimensional array, for example, it will essentially âgroup byâ each unique value, taking
the first value of the other rows.
keyBy($keyBy) - allows you to pull out one set of data from your items and make that the key.
random($amount = 1) - grab a random element or collection of elements (if more than one is asked
for)
first(callable $callback = null, $default = null) - grab the head() of a simple array, or the
of the filtered result if you use a callback.
last(callable $callback = null, $default = null) - grab the tail() of a simple array, or the of
the filtered result if you use a callback.
19. Ways to loop, transform and misc.
methods
Letâs close things out with a few odds and ends that might be useful to know about.
Grouping:
This is one of my favorites. Remember how you would query all the user types, then loop over them
and gather each set of users so you could make nice reports with the proper heading? Forget all that:
1 dump( collect([
2 ['name' => 'Jeff', 'type' => 'programmer'],
3 ['name' => 'Gerry', 'type' => 'designer'],
4 ['name' => 'Joe', 'type' => 'programmer'],
5 ])->groupBy('type') );
Looping:
each(callable $callback) - this works very much like the javascript version of its name. Instead
of creating a loop with
1 foreach ($collection as $item){
2 // do something
3 }
we can call it in a more mapping fashion, including chaining:
1 collect([1, 12, 32, 14, 5])->sort()->each( function($item, $key){
2 echo $item . '-';
3 });
Another interesting option is every(), which allows you to make a subset of the collection:
1 dump( collect([1,2,3,4,5])->every(2,1));
2 // yields an array(2,4)
20. Ways to loop, transform and misc. methods 17
Pagination:
Laravel has a wonderful set of pagination functions that work with the QueryBuilder and Eloquent.
That said, there are times when you may wish to use IlluminatePaginationPaginator to create
your own paginator. Most likely you will still want to add limits and offsets to your query, but
occasionally you may prefer to cache a complex resultset and slice it. forPage($page, $perPage)
uses array_slice() but helps you by automatically calculating the lenths and offsets so you can just
work with clean code.
take($limit) also uses array_slice(), but in a simpler form to bring back the requested number of
items. Use a negative $limit to grab from the end.
forget($keys) will simply unset() the keys from the original collection. If you need to preserve
the original, be sure to call this on a copy.
Transformation:
These functions are for âbrute forceâ collection of all the values present, whether a single level deep or
multi-dimensional. I imagine their greatest use would be in searching or tracing through a complex
multi-level hierarchy.
collapse() - turns a collection of arrays into a single collection of all values.
flatten() - like collapse(), but works on multi-dimensional collections.
zip($items) - will array merge on keys; i.e.,
collect( [1,2,3] )->zip( [4,5,6] );
Results: array( array(1,4), array(2,5), array(3,6) )
transform(callable $callback) - this is just like map(), with the difference being that map()
returns a new collection with the callback results, but transform() actually applies them to the
original collection.
21. Support collections vs. Eloquent
collections
Working through these examples youâre sure to have noticed a lot of familiar method names from
other objects. The Request and Config (actually the IlluminateConfigRepository) are two that
should come to mind, but the framework is full of them. However, if you look more closely youâll
see that these are nothing more than âcousinsâ of the Collection - they all implement ArrayAccess
and use IlluminateSupportArr class, but have no tighter relationship than that.
The Eloquent Collection, on the other hand, is a directly inherited class. All of the methods reviewed
in this book are available, although a few have been overridden to function slightly differently, as
well as some others added that are only available in the child class. To be honest, this can make
things rather confusing at times.
We will limit how deeply we get into the Eloquent Collection class (perhaps a second edition of this
book will spend more time here), but there are some areas we definitely want to visit if we are going
to fully master Collections.
Our first indication that something is different can be seen with the following experiment:
1 $users = AppUser::all();
2 dump($users->find(2));
3
4 $users = collect(AppUser::all());
5 dump($users->find(2));
The first dump() finds the model we are searching for; the second throws an error since Illumi-
nateSupportCollection::find() doesnât exist. There is no great mystery behind this - Eloquent has
its own set of functions and classes, and while we say that AppUser::all() returns a collection,
we really mean the Eloquent Collection subclass is being called. Confusing, though, if you are used
to referring to everything as a âcollectionâ without qualification.
While most of the differences are minor and pertain more toward making the collection object work
with a group of Eloquent models, there are a few functions that you might want to know about and
that are not (currently) documented:
add() - this works just as push() does, and adds a model onto the end of the collection
modelKeys() - this will grab the full set of returned primary keys, either id by default or whatever
you may have designated in your model.
22. Support collections vs. Eloquent collections 19
only() and except() sound just like the Request object methods of those names, but actually work
on the primary keys to filter the models
withHidden() - will include hidden fields in the results
23. Appendix - Laravel 5.2 changes
Laravel 5.2 is coming out in December (2015) and there are a lot of nice new features to be
excited about. Collections are no exception - there are several new functions I think you will really
appreciate.
The first two are âborrowedâ from the Request and Eloquent Collection objects. I say âborrowedâ
in quotes because really these two are based on the Arr class and have just been given wrapper
functions on the SupportCollection. They are mirror images of each other:
except()
1 Ex. - $arr = Collection(['name' => 'Jeff', 'twitter' => 'codebyjeff']);
2
3 dump( $arr->except('name') );
4
5 // result: array{ 'twitter' => 'codebyjeff' }
only()
1 dump( $arr->only('twitter') );
2
3 // result: array{ 'twitter' => 'codebyjeff' }
A small change has been made to prepend() so that you can give it a key as well as a value, such as
(taken from the test6
, which is an excellent way to study these!):
1 $c = new Collection(['one' => 1, 'two' => 2]);
2
3 $this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $c->prepend(0, 'zero'
4 )->all());
Just remember it is $value, $key and not the other way around like youâd think with an array.
Function flatMap() combines mapping with the flatten() function, allowing you to write essentially
a custom callback to retrieve nested arrays, then collapse them into a single dimensional array. This
might be used, for example, to look through all of the usersâ choices and return a list of them.
6
https://github.com/laravel/framework/blob/5.2/tests/Support/SupportCollectionTest.php
24. Appendix - Laravel 5.2 changes 21
1 $data = new Collection([
2 ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
3 ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
4 ]);
5
6 // this callback simply returns the 'hobbies', but could do anything
7 // you might do in an array_map
8
9 $data = $data->flatMap( function ($person) {
10 return $person['hobbies']; }
11 );
12
13 $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $dat
14 a->all());
Finally, one of the simplest yet potentially most interesting is macroable(). Simple, because it only
requires the addition of the Trait Macroable; potentially most useful because it allows you to create
your own Collection-level scopes, putting together any chained collection functions along with any
other business logic you may wish to add.
1 // Foo() macro : unique values starting with A
2
3 Collection::macro('foo', function () {
4
5 return $this->filter(function ($item) {
6 return strpos($item, 'a') === 0;
7 })
8 ->unique()
9 ->values();
10 });
11
12 $c = new Collection(['a', 'a', 'aa', 'aaa', 'bar']);
13
14 $this->assertSame(['a', 'aa', 'aaa'], $c->foo()->all());
When I read this, the first thing that came to mind was stored procedures or business intelligence
objects to encapsulate business logic for the other developers to make use of. Iâm interested in seeing
examples of how people are using it - go ahead and grab a copy of 5.2 dev and let me know your
ideas!
25. Appendix - Laravel 5.3 changes
Laravel 5.3 should be out in a few weeks (June 2016) and there are a couple of new functions and
several changes you should know about.
First, three new functions:
diffKeys(): A wrapper around array_diff_key. Get the items in the main collection whose keys
are not present in the second one. Notice we are talking about keys, not values (which can be found
with the already existing diff()). For example:
1 $collection1 = collect([1 => 'red', 2 => 'green', 3 => 'blue', 4 => 'orange']);
2 $collection2 = collect([2 => 'red', 3 => 'pink']);
3
4 dump($collection1->diffKeys($collection2));
5
6 // result: array:2 [ďż˝
7 1 => "red"
8 4 => "orange"
9 ]
How might this be used? If you recall the getDictionary() function on the Eloquent/Collection,
this will set up an array of models where the keys are the modelsâ primary keys. diffKeys() would
then let you quickly and easily compare two result sets from your data source.
union(): Union simply concatenates the two collections, giving preference to the parent when there
is a conflict.
1 $collection1 = collect([1 => 'red', 2 => 'green', 3 => 'blue', 4 => 'orange']);
2 $collection2 = collect([2 => 'red', 3 => 'pink']);
3
4 dump($collection1->diffKeys($collection2));
5
6 // result: array:4 [ďż˝
7 1 => "red"
8 2 => "green"
9 3 => "blue"
10 4 => "orange"
11 ]
26. Appendix - Laravel 5.3 changes 23
You can see that since both collections have the keys 2 and 3, the values of $collection1 are
maintained. Essentially all this is doing under the hood is to use the array forms of the two collections
so they can be concatenated normally. Identical to:
dump($collection1->toArray() + $collection2->toArray());
combine(): A wrapper for the array_combine combine function that uses the values of the parent
collection as keys and the second collection as values. As expected, thee are a few things to watch
for:
1) If both collections do not have the same number of elements, an exception is thrown.
2) If the parent collection is a set of Models, each will be json encoded to create a string for the key,
with the matching model from the second collection as the value:
1 $user1 = AppUser::find([1]);
2 $user2 = AppUser::find([2]);
3
4 dump($user1->combine($user2));
5
6 //result:
7 array:1 [ďż˝
8 "{"id":1,"name":"Jeff","email":"jrmadsen67@gmail.com", ...}" => User {#157 ďż˝}
9 ]
The next set of changes all center around where and its variations - whereLoose, whereIn, and
whereInLoose.
The biggest of these is where; instead of the fixed where($key, $value) where only â=â can be used,
it has been changed to:
where($key, $operator, $value = null)
Fear not! All of your old code has not been broken. The function has been âmonkey-patchedâ so that
if only two arguments are given, it will continue to work as is, but if three are used it will assume
the new, more flexible format. There is a function called operatorForWhere() that shows you all of
the accepted $operator values.
In addition:
1) whereIn now uses loose comparisons by default; there is a new whereInStrict
2) whereIn will accept any arrayable objects, not just arrays, as the second argument
Finally, there is a small change to the keyBy() function when passing closures, which now works
with both the key and value, such as in this test:
27. Appendix - Laravel 5.3 changes 24
1 public function testKeyByClosure()
2 {
3 $data = new Collection([
4 ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
5 ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
6 ]);
7 $result = $data->keyBy(function ($item, $key) {
8 return strtolower($key.'-'.$item['firstname'].$item['lastname']);
9 });
10 $this->assertEquals([
11 '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'l
12 ocale' => 'US'],
13 '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'lo
14 cale' => 'FR'],
15 ], $result->all());
16 }
As always, interested in hearing how you put these changes to use!
28. Appendix - Laravel 5.4 changes
Laravel 5.4 is on the verge of being released (Jan 2017), so all of the communityâs package maintainers
and bloggers are busy getting their ducks in a row. To paraphrase Taylor, 5.4 is a bit more focused on
âhousecleaningâ than any major announcements, and the changes to Collections are equally simple.
As always, though, there are a few nice additions to know about.
There is a breaking change you want to be aware of with the every() function. What was previously
âevery nth itemâ is now called, fittingly, nth() . The every() function is now used to check that every
item in the collection passes a given truth test.
The random() function will now return a Collection even if only a single item returned. This is
consistent with the upgrade from 5.2 > 5.3, but youâll want to be sure you are always expecting a
Collection class and never a single model.
There are a couple of new functions, as well. ContainsStrict() works just like the contains() function
to see if an item is found in the collection, but uses strict comparison.
Split() is similar to chunk() and (in fact, relies on it internally) but instead of telling it the size of the
groups you want returned, you tell it how many groups. Be careful as the last group may be smaller
than the others.
MapWithKeys() allows you to return an associative array from your mapping, so you can now pull
two items from a record and set up on of them as the key in the return value. The docs explain that
better, so let me borrow here:
1 $collection = collect([
2 [
3 'name' => 'John',
4 'department' => 'Sales',
5 'email' => 'john@example.com'
6 ],
7 [
8 'name' => 'Jane',
9 'department' => 'Marketing',
10 'email' => 'jane@example.com'
11 ]
12 ]);
13
14 $keyed = $collection->mapWithKeys(function ($item) {
15 return [$item['email'] => $item['name']];
16 });
29. Appendix - Laravel 5.4 changes 26
17
18 $keyed->all();
19
20 /*
21 [
22 'john@example.com' => 'John',
23 'jane@example.com' => 'Jane',
24 ]
25 */
Some of you might recall the calisthenics Adam Wathan had to jump through to set this up in his
Refactoring to Collections book.
Finally, a small but helpful isNotEmpty(). As you suspect, it is just a wrapper around return ! $this-
>isEmpty(); but giving it its own function should help make a little cleaner code in some of your
callbacks, etc.
That covers most of the significant stuff. You can look through the commit list to get all the specific
details, and the docs are actually already up although possibly still being finalized.
Hope that helps!
30. To be ContinuedâŚ
Yes, to be continued. New versions of Laravel will come out; new uses will be found for this material.
I hope this was worth the read, and that it has piqued your interest to explore!
Thank you very much for reading.
Jeff Madsen
Codebyjeff7
@codebyjeff8
7
http://codebyjeff.com
8
https://twitter.com/codebyjeff