DEV Community

Cover image for 💡Block
Ken Okabe
Ken Okabe

Posted on • Edited on

💡Block

image


Introduction to outline of functional programming


  • Functional code is assembled like LEGO blocks

  • The basic principle of LEGO blocks is to "connect two blocks together to create a new block"


  • The blocks of functional code are actually expressions (mathematical expressions)


  • The way LEGO blocks are connected is "fitting the bumps and holes", which is the only way, but the way blocks of expressions are connected is infinite

  • When LEGO blocks are connected by "fitting the bumps and holes", of course the "shape and size of the bumps and holes" must match, and such rules are called standards in the industrial world, and the USB standard is well known

image

Amazon affiliate to do UGREEN USB Type C Cable for Smartphone L-Shaped Nylon Braided 3A Fast Charging Quick Charge 3.0/2.0 Compatible 56K Resistor Installed Xperia XZ XZ2, LG G5 G6 V20 etc. Compatible (1m)


  • When two expressions (blocks) are connected by an operator, the operator and the expressions must match the standard, which is called standard in the industrial world, but in the world of programming, it corresponds to something called Type

The similarity between the "standard" of the industrial world and the "type" of the programming world

What is the "type" of the programming world, or more precisely, the "type" as a mathematical entity? We will dig deeper into this later, but as a rough image for the first entrance, you can understand it as something like the "standard" of the industrial world


image

image

image


  • Since there are infinite ways to connect blocks of expressions, there are also infinite types

  • Among the infinite ways of connecting blocks of expressions, the four methods of addition, subtraction, multiplication, and division are taught in elementary school arithmetic and are called arithmetic operations

image


  • This way of connecting two expressions (blocks) is called binary operation, and arithmetic operations are members of binary operations

  • The (binary) operators that connect two expressions (blocks) are  +− × ÷~+ - ~ \times ~ \div

image


  • The type that matches the expressions (blocks) to be connected is a number
1 2 3 4 1 ~ 2 ~ 3 ~ 4
  • A binary operation that connects two expressions (blocks) 11 and 22 with the operator ++
1+2=3 1 + 2 = 3
  • A binary operation that connects two expressions (blocks) 33 and 44 with the operator ×\times
3×4=12 3 \times 4 = 12
  • 1+2=31 + 2 = 3 is a new expression (block) that is assembled by connecting two expressions (blocks)

  • Therefore, as long as you can connect two expressions (blocks), it is just a repetition of the same thing, and you can freely connect three or more expressions (blocks) by combining the same connection ++ or different connections ×\times , etc.
(1+2)×4=12 (1 + 2)\times 4 = 12
  • Functional programming is a method of doing everything only by connecting and assembling blocks like this

  • So in the end? Functional programming is just building mathematical expressions like (1+2)×4=12(1 + 2)\times 4 = 12 , so it can be said to be mathematics itself

  • Since we have already done this kind of operation in arithmetic and mathematics classes, we have already mastered the basics of functional programming! 🥳🎉

Programming languages where code is composed of expressions

Relatively new programming languages such as Rust and Verse have a strong functional character, and in principle, code is composed of expressions

image

Statements and expressions

Rust is primarily an expression language. This means that most forms of evaluation produce or cause a value, and evaluation is guided by a unified syntax category called expressions. Each kind of expression can usually be nested within other kinds of expressions, and the rules for evaluating an expression specify both the value it produces and the order in which its subexpressions are themselves evaluated.

In contrast, statements are primarily used to contain expressions and sequence their evaluation.

image


image

image


As explained in the references of Rust and Verse, which emphasize that code can be written in expressions at the beginning, the content is exactly the same as what was introduced in this chapter, and it is the most basic and fundamental nature of functional code

Top comments (0)