Learn Red – Fundamentals of Red
eBook - ePub

Learn Red – Fundamentals of Red

Get up and running with the Red language for full-stack development

Ivo Balbaert

Share book
  1. 252 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn Red – Fundamentals of Red

Get up and running with the Red language for full-stack development

Ivo Balbaert

Book details
Book preview
Table of contents
Citations

About This Book

Discover how to use the next-generation language Red for full-stack development, from systems coding over user-interfaces to blockchain programming

Key Features

  • Explore the latest features of Red to build scalable, fast, and secure applications
  • Learn graphical programming and build highly sophisticated reactive applications
  • Get familiar with the specific concepts and techniques of Red development, like working with series, viewing code as data, and using dialects.

Book Description

A key problem of software development today is software bloat, where huge toolchains and development environments are needed in software coding and deployment. Red significantly reduces this bloat by offering a minimalist but complete toolchain. This is the first introductory book about it, and it will get you up and running with Red as quickly as possible.

This book shows you how to write effective functions, reduce code redundancies, and improve code reuse. It will be helpful for new programmers who are starting out with Red to explore its wide and ever-growing package ecosystem and also for experienced developers who want to add Red to their skill set.

The book presents the fundamentals of programming in Red and in-depth informative examples using a step-by-step approach. You will be taken through concepts and examples such as doing simple metaprogramming, functions, collections, GUI applications, and more. By the end of the book, you will be fully equipped to start your own projects in Red.

What you will learn

  • Set up your Red environment to achieve the highest productivity
  • Get grounded in Red, gaining experience and insight through many examples and exercises
  • Build simple, compact, and portable applications
  • Analyze streams of data through Parse
  • Compose GUI applications with View and Draw
  • Get prepared for smart contract blockchain programming in Red

Who this book is for

This book is for software developers and architects who want to learn Red because of its conciseness, flexibility, and expressiveness, and more specifically for its possibilities in GUI apps and blockchain / smart contracts programming. Some knowledge of the basic concepts and experience of any programming language is assumed.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learn Red – Fundamentals of Red an online PDF/ePUB?
Yes, you can access Learn Red – Fundamentals of Red by Ivo Balbaert in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789133653
Edition
1

Using Functions and Objects

This chapter is about modularizing code through the use of functions and objects.
We have already encountered a lot of built-in functions, such as print and cd, but of course you can write user-defined functions as well. When a program's code gets longer and more complex, you want to be able to give a name to certain code segments. For example, when processing a data stream from a file or the network, you'll want to have a process-request or process-record function that contains the logic of dealing with one chunk of data. That way, you can call this function each time you need it. This leads to less code that is better structured and more readable. Less code means fewer bugs! Functions can take parameters, local variables, and refinements, and we'll be exploring how to use these later on in the chapter.
Another way to group code is to define an object. Red is not class-based, like most object-oriented languages in common use today, such as Java, Python, C#, or Ruby, it takes a more prototype-based approach, where new objects can be created on the basis of an already existing object—its prototype. In this chapter, we will explore what you can do with objects in Red in depth.
In this chapter, we will cover the following topics:
  • A fauna of functions
  • Function attributes
  • Working with functions
  • Code is data and data is code
  • Using objects

Technical requirements

You'll find the code for this chapter at https://github.com/PacktPublishing/Learn-Red-Fundamentals-of-Red/tree/master/Chapter06. If you have installed Red as indicated in Chapter 2, Setting Up for Development, you are good to go. You can work on Windows, OS X, or Linux. You can type or paste any code in the Red console to see its results, or you can use an editor, such as Visual Studio Code.

A fauna of functions

Red's built-in functions can be categorized as follows. The type is indicated within ():
  • operators (op!), which can be used with infix notation, such as a + b, a / b, and so on.
  • native (native!) functions, such as if, either, while, throw, all, wait, and so on; you can see a complete list by using ? op! in the console.
  • routine (routine!) functions, such as exists?, write-clipboard, and so on; you can see a list using ? routine! It is also used in Red to define a function that calls a Red/System function (see Chapter 10, Advanced Red).
  • action (action!) functions, such as copy, move, clear, to, form, and so on; you can see a complete list using ? action!
The native, routine, and action functions are written in Red/System. Action functions are special in that they are polymorphic—they are defined for more than one datatype. Which action code is executed depends on the type of its first argument.
  • mezzanine (function!) functions, which are higher-level functions written in Red; you can see a complete list using ? function!
In this section, we'll cover the variety of ways that exist to define your own functions.

The do word

We will start by giving a name (a label) to a code block so that you can call that code by its name and evaluate it with the do word, which we already saw in action in the Evaluation with do and reduce section in Chapter 3, Using Words, Values, and Types. Here, the word pri5 is bound to a piece of code, which prints five digits separated by dashes:
;-- see Chapter06/do-does-has-func.red:
pri5: [
repeat i 5 [prin i prin "-"] ]
do pri5 ;== 1-2-3-4-5- ; evaluate code block with label pri5
t...

Table of contents