Posts

Showing posts with the label FP

Functional Programming

Image
There are many programming languages. Most of them are based on C. However, there are a few different schools of thought. There are imperative, object-oriented, and functional schools and a couple of branches and variations, but that's the mainstream of languages you will see in production in big companies. Programming languages is how we express our intents and turn that intent into value for people. Usually, people are concerned about how other engineers would read the code, make sense of it, and be able to effectively maintain it. System programming languages like C and Zig tend to focus more on performance over abstractions (which always have a cost). Object-oriented programming languages like Java focus more on structure and how to abstract things. Abstractions are not necessarily bad if done right. They have a cost and impact on the people who maintain them. Having more abstractions on the SDK or standard library is a good thing for mantainance and increases people's kno...

Closures in Rust

Image
Rust allows us to do functional programming. One important thing in FP is being also to pass functions as parameters and also return functions which are also called high Order Functions. Today I want to share 2 videos and 2 pocs showing how we can do High Order Functions in Rust also you will get currying for free :-). I also will show how we can do closures using traits as well. So Let's get started!

Fat Jar with Scala 2.13 and Sbt 1.5

Image
Fat Jar is a very useful technique. Having one jar and one jar only makes the distribution of software much easier and also mantaince much more sense as you need to roll out bug fixes and changes. Scala allows us to create self-package, self-executing one single fat jat by using the Assembly plugin. Today I want to share a simple POC and video I made showing how we can create a Fat Jar using Scala, Assembly, and Sbt. The code will be using a java dependency, a project called Apache Commons Lang .  So let's get started! The Video The Code https://github.com/diegopacheco/scala-playground/tree/master/fatjar-assembly-sbt-1.5.0 Cheers, Diego Pacheco

Scala 3.0

Image
Scala 3.0 is not out yet but has some interesting new features. I'm still digesting some features some I have some mixed feelings about it. One of the main big changes is the support for Python-like syntax. Scala 3.0 has lots of shortcuts, types of convenience, and different ways to do generics. Scala always had the philosophy to be flexible on the language level and let the tools and companies restrict functionality, so this still pretty much very true. I made a series of 3 videos going over some of the changes in Scala 3. This material is highly inspired by the scala 3 boo/docs. Some code and features on the videos will not be limited by scala 3 but also cool things about scala 2 or even scala in general but mostly I will be talking about scala 3.0. Keep in mind scala 3 is not final so things could change but I believe you gonna be able to have a good sense of what to expect and what's going on. So Let's get started. 

Functional Programming in Kotlin

Image
Kotlin is an interesting JVM language. Today I want to show some capabilities Kotlin has around Functional Programing. FP is a very interesting way to think and create safe programs. Today we will cover High Orde Functions, Lambdas, and also Class extensions. So today I made a simple video and code POC. Let's get started! The Video The Code https://github.com/diegopacheco/kotlin-playground/tree/master/kotlin-fp-fun Cheers, Diego Pacheco

Scala Accord

Image
Accord is an interesting lib for validations. Accord is composable, simple, self-contained, and integrated with ScalaSpecs for instance. In Java, validations are often done via annotations. In general, there is an abuse of annotations in Java mostly because there is a lack of Macros which we have in Scala and Rust for instance. I really like the Accord approach to validations where we dont throw Exceptions and we can extend and re-use validations across the type system. So today I made a simple code with Scala and Accord using SBT as-built system and a video to explain in more detail. So let's get started. 

Rust Parser and State Machine

Image
Rust it's a very nice and powerful language. I believe Rust has a place beyond systems and hardware programming. I personally believe we will be seeing more and more Rust in the following years just like Go took some time to liftoff I believe Rust time is yet to come. Rust it's powerful because favors safe programming not only in sense of memory management but also combined with functional programming we can get the best of all words meaning: Safety, Productivity, Less code, Doing more with less, reducing complexity, and leverage engineering best practices. Today I want to show how easily we can write a cyclic state machine parser with Rust and without using any crates but really 100% on the standard language.  So Let's get started.

Java Collectors

Image
To be 100% clear this post is not about Java Garbage collector neither Java getting your money :D. Java 8 introduced Collectors, but still most engineers just barely know them. Collectors are pretty useful if you are working with Streams. According to the book Effective Java 3rd edition, you should not abuse from Streams but unfortunately thats, not the reality. However, if you still have fit you stream code in a few lines of code Collectors can be handy and save you some additional transformation steps. Most people when they know collectors they tend just to use the toList() collector but there is much more to explore. Today I recorded a video showing some other collectors' features with you guys. I hope you like it. Let's get started. 

Property Based Testing with Jqwik

Image
Property-Based Testing in a really nice functional programing way todo testing. Originally created at Haskell with QuickCheck . Today there are frameworks for pretty much all languages.  Jqwik is a property-based testing framework for java.  Property-based Testing is cool because you focus on a predicate / boolean expression and forget about inputs. When I say forget about, thats happen because the property based testing takes care to generate all possible inputs for you and you can save lots of typing, creating uses cases. You still need to work in your property/assertions however you end up writing much less code and cover a much wider range of possible inputs.  Today I want to share a video I made about it, so let's get started.

Having fun with Cats

Image
Cats is a library for Functional Programming in Scala. The name come from the Inspiration by the Category Theory  so Cats is short for Category Theory. Cats are about types, Type System and having abstractions to make it easy to work with them. If you want to go to the next level of function programming Cats is the way to go, however, I need to say that it's hard, get the ideas to take time, understand the terms, principles, and applications takes times as well. At the end of the day, this is about style so there is no right or wrong. Recently I made a post about Monads so it might be useful for you to read it if you did not read it yet. This post won't be monad heavy but reading about monads will make it easy to understand some things here. It's fine if you don't understand everything, I'm 100% sure I don't understand everything and there are lots of ideas and concepts that I still learning. So this takes time. I don't think this is something...

Scala Monads 101

Image
What is a monad ? It's easy to read lots of sites and still don't get the idea. Many people already used monads but few really understand them truly. Monads are a kind of a Myth in Scala for lots of developers. ItÅ› possible to code without creating your own monads? Yes for sure it is. It's very likely you already create abstractions which were similar to monads but with different names. Here are some samples of Monads in Scala: Try , Either and Option . We have some of this monads in Java 8 as well like Optional . Monads come from the Category Theory which is a branch from Math. Monads are very strong concepts in Haskell Language for instance. IMHO one of the reasons why use Monads rather than your uncle abstraction is that they are universal and in theory, they are easier to understand, in practice if you don't study math, Category Theory and Functional Programming principles actually monads will sound much harder than you user uncle abstractions. So, in other word...

Code for Reliability

Image
It's normal to talk about failure when we talk about cloud-native architectures. Pretty much anything that runs on the cloud(data center) means more distribution(networking). Distributed Systems tend to fail all time. Chaos Engineering is great, however, it's for the infrastructure, therefore focused on the big picture. When I say big picture I mean outside of a microservice. Reliability is not just for the outside boundary or infrastructure but also for Inside a service or inside the system. Reliability Inside the system might have many names such as(very popular in Brazil during 2k years) Defensive Programming .  There are lots of synergy between reliability , defensive programming, anti-fragility and Efficient Internal System design. Efficient design it's not only about making your system efficient in sense of economics(code, readability, reduce maintained cost) but also reliability. Today I want to share some internal system design way of think in order to ach...

The power of Scala Type Classes

Image
Scala language it was very inspired by Haskell . Type classes in another sample of this and many inspirations  from Haskell. There are always several ways and styles of thinking to address problems. Type classes is another way of thinking and dealing with problems. For some problems, you might just use Pattern Matcher -- But if you have to add a type? Right, I could stick with good old OO since Scala is hybrid right? --  But what if you need to add one operator? Type Classes can help you out.

Functional and Reactive Programing with Java 8

Image
Unfortunately, FP is a religious suggest for a lot of people. I jumped on the FP Religious bandwagon for awhile back into 2012. Today I consider myself more practical rather than just "pure" in regards of FP. To be 100% clear my goal is not to be controversial or create any kind of flamewar. I feel free to disagree with me. The fact is I like Scala a lot and is very hard to talk about java 8 without talking about Scala. IMHO Java 8 got some good influence from Scala. IMHO I don't think Java 8 is a drug to get into Haskell :-). Java 8 got some sort of FP influence, Thanks to Scala I would say. I suppose to deliver a talk about functional and reactive programming with Java 8, 2 years ago and I found this slide deck in my machine so I thought about making a video presenting some of the functional programming principles with Java 8 including frameworks such as RxJava. So this is a ~40 minutes video - I hope you guys enjoy and have fun.

Core FP Concepts slidecast

Image
Functional programing is growing a lot now a days, not only pure function programing languages but several new languages are hybrid or at lest had fp influences like java, javascript, c#, python.  On this slidecast i will talk about some of the core concepts on functional programing, here you want find all concepts but this could be a starting point for you. I hope you have fun :-)

Scala Package Objects

Image
So lets talk about this feature most people dont know scala has is the ability to use package objects. You might dont find that useful since you have do the same with different techniques but i think this is great for the granularity scala have to control visibility. You can use this feature to create DSL or simple to empower abstractions this feature is compiled so its safe but could lead to bad code if you dont use it with window. Because you can be sharing variables everywhere so this is really useful for module design but  as i sad you need o be careful.

Clojure Protocols

Image
So if you remember my last posts: *   Pure OO, Inversion of Control(IoC) + Dependency Injection(DI) and The Cake  Pattern(FP): Whats really matters? *  Inheritance + Mixins *with* another solution I mentioned closure protocols as an option for the design problems i was talking bout but i didn't not show any code or explain much around it. So this is basically one another approach for you to design your software. 

Inheritance + Mixins *with* another solution

Image
In previous post i was talking about some alternatives to classical IoC/DI. I`m not here to argue whats bets or worst, the 2 approaches have advantages and cons. Most classical OO languages like JAVA and .NET tend to fix things at runtime. Java for instance all happens in runtime, spring context. JSF context, Hibernate/JPA context, reflection(memory), CGlib, proxies, etc.. In FP languages that are children of haskell like F# and Scala things tend to happen as much as possible close to the compiler and thats has good and bad things like everything in life. In java or .NET you dont have multiple inheritance like you do in C++. So another approach would be do inheritance and work with the ideas of Modules. Some people call this: Thin Cake Pattern, for me is just multiple inheritance with syntactic sugar. As long as you do it only for classes/traits and dont do it for values i think is ok and IMHO cleaner than the cake pattern.

Pure OO, Inversion of Control(IoC) + Dependency Injection(DI) and The Cake Pattern(FP): Whats really matters?

Image
Software development continues to improve, once you add new languages, design concepts, paradigms(like functional programing) we start having new ways of doing things, so whats really matters? should i change somethings and embrace everything or should i change nothing? In this post i want cover something if you was java developer was the central idea around java development for years. Spring Framework dominate the fields for more than a decade. The real question is not if  you should be using XML or annotations(like Guice ). Either if you should go XML or java Annotations. Thats kind of old discussion, so basically what are the options now a days?

SOA has Roots in Agile and Functional Programming

Image
SOA it`s about software architecture so its about an overall solutions for a business.  Thats what the 'A' stands for. Behind this we got Service Orientation as the main paradigm of doing some, its a way of thinking and it has principles like OOP, FP and Agile. SOA got popular around 2k years when business was moving to more complex functionality and more and more data. Them it came several buzzwords and vendor-like things, but soa was never about technology was always about principles and a specific way to think, see and build things, the service orientation. To capture this view a group of awesome people created the soa manifesto .