Internet of Things (IoT) and Blockchain

While news agencies talk about whether Bitcoin is a bubble or not, we are witnessing an important breakthrough with the developments that blockchain technology brings about to our life. Well, it is not visible for now but once we see the fruits of blockchain technologies, I reckon that we will no longer be talking about Bitcoin, but what blockchain is.

Directly quoting from Wikipedia, it is:

“A blockchain facilitates secure online transactions. A blockchain is a decentralized and distributed digital ledger that is used to record transactions across many computers so that the record cannot be altered retroactively without the alteration of all subsequent blocks and the collusion of the network. This allows the participants to verify and audit transactions inexpensively. They are authenticated by mass collaboration powered by collective self-interests”

To put it simple, blockchain technology allows peer-to-peer interaction. I call it interaction but not transaction as I believe that it opens up more opportunities than allowing a secure transaction without an agent. I call it interaction as it could be anything such as p2p lending, transacting, sharing, trading, publishing, tracking. This list can be expanded and blockchain technology allows such actions to be performed in a secure, fast, reliable and, most importantly, in a decentralised way. Mark Zuckerberg, the founder of Facebook, also recently acknowledged the decentralised revolution that blockchain brings about to our life (Ironically, he owns the one of the most centralised company which savagely uses personal data to make money)

Blockchain technologies nowadays, unfortunately, are only mentioned from the economical aspect. To explain it to those who is strange to crypto space, there are exchanges and people buy and sell, what they call “coins” to one another. These coins or more accurately blockchain projects offer their concepts to the community and pursue a wild marketing to be able to increase the value of their coin and be visible in that space. As speculation is the main trigger of this space, blockchain technology and its advantages is hardly mentioned among the community. There are thousands of projects all of which claim to create so-called “revolution”. While a few of them seemed to or will accomplish this, the majority of them only stand out with the marketing tactics rather than the technology behind it.

You may only know Bitcoin or Ethereum, but there are other serious blockchain projects that will try to survive long years and contribute to IoT concept rather than explode in the crypto bubble. To name some of those: Steem, Stratis, Waves, IOTA, Waltonchain, NEM, NEO, Omisego. I know, they all sound weird if you only heard about Bitcoin and how its value increased over years but the ones I named here and other serious projects will become a part of our life. We will be aware of with some of them as they directly touches our everyday life (Steem is one of them. It is a decentralised social media platform where community rewards quality content with Steem tokens). There are also others that will join our life without that much visibility. To illustrate what they are trying to accomplish, we can look at two distinctive projects like IOTA and Waltonchain.

IOTA is one of the projects that was inspired by blockchain technologies but developed its own concept. IOTA uses a system that is called “Tangle”. The idea of tangle is that as they underline in their whitepaper:

“…to issue a transaction, users must work to approve other transactions. Therefore, users who issue a transaction are contributing to the network’s security. It is assumed that the nodes check if the approved transactions are not conflicting. If a node finds that a transaction is in conflict with the tangle history, the node will not approve the conflicting transaction in either a direct or indirect manner”

They claim that Tangle offers much faster processing power than blockchain technology. Their target area is machine economy, where IOTA tokens can be used for machine-to-machine transactions. When IOTA economy merges with artificial intelligence, it would not be a surprise that cars automatically pay their parking tickets. While I oversimplified what they want to manage, you will be mesmerised what these technologies will achieve in the future. This could be IOTA or any other project but it will definitely be part of our life.

Waltonchain is another project that aims to use blockchain technologies in a much different and revolutionary way. Waltonchain is located in China and also operates in Korea which seems to give them a priceless opportunity to merge blockchain and RFID chips in supply chain management. What distinguish Waltonchain from other projects is that it is claimed to be a hardware and software project that manufactures RFID chips to be used with their blockchain, as they underline in their whitepaper

“The whole system of the Value Internet of Things can be divided into two parts: hardware and software. The hardware includes the RFID tag chips and the RFID reader chips. The RFID tag acts as the interface for all assets to be connected to the chain, and the reader chip is a bridge for all assets to be connected to the chain and can be used as a node on the chain. The software includes the Waltonchain software system, the Walton protocol and the Walton coin. With the combination of software and hardware, the Value Internet of Things can really achieve the connection of all things to the chain and the digitalization of all assets”

As China is the motherland of supply chain and Korea can be said to be famous with chip technology, it seems that Waltonchain will be one of the projects that will survive when the “cryptobuble” bursts. I do not want to dive into technical details but projects like Waltonchain gives us real good use cases regarding how blockchain technology can be utilised other than being used as a currency. Their smart city project and the use of Waltonchain in supply chain industry should be more recognised, appreciated and discussed, instead of what Bitcoin price will be.

I illustrated only two projects among many quality ones. I believe, if we compare dotcom bubble with crypto bubble, there will be similar outcomes: some will burst and be forgotten, some will survive and be the pioneer in their area of expertise.

Source: https://medium.com/@ahmetdurgungoz/interne...

How to use Kotlin's 'it also let apply run'

Kotlin is being officially used in Android development, and every Android developers are probably busy picking up Kotlin. That includes me.

I stumble upon these few magical methods during my Kotlin journey:

    .also()
    .let()
    .apply()
    .run()

They are magical because they can perform some Kotlin magics and at the same time greatly resemble English words. Thanks to the resemblance, I even tried forming sentence using them. Let's assume Apply is a person name, I can make a grammatically correct English sentence with them: it also let Apply run.

Nonsense apart, I find it really hard to understand the usage based on their names.

There are also with and other friends in the Standard.kt, but I want to keep this post focus. So I'm leaving out the rest. Actually I'm just lazy to cover them all ಠ_ಠ. I want to go do some snowboarding instead, it's winter already, yay! ^3^

1. let and run transform

1a. pug analogy, part I

There's a famous saying "to begin learning is to begin to forget". So let's forget about it also let apply run for a second. Ok, I just made that up. Let's start with a simple requirement.

Let's say you have a pug.

and you want to add a horn to it.

Here's the code for doing this.

val pug: Pug = Pug()
val hornyPug: HornyPug = putHornOn(pug)
fun putHornOn(): HornyPug {
   // put horn logic
   return hornyPug
}

Now it has became a pug with horn, let's call it hornyPug:

From pug to hornyPug, the original pug has changed. I call this "transformation".

Let's re-write this using run

val pug: Pug = Pug()
val hornyPug: HornyPug = pug.run { putHornOn(this) }

Here's re-write with let

val pug: Pug = Pug()
val hornyPug: HornyPug = pug.let { putHornOn(it) }

1b.Function definition

Take a look at the Standard.kt for the how let and run is written:

public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
public inline fun <T, R> T.run(block: T.() -> R): R = block()

It can be hard to read at first, let's only focus on the return type for now:

  • R is the return type
  • T is the input or type of the calling object.

What it means is T type will turn into R type after let or run.

In the case of our example, pug is T, hornyPug is R.

1c. Key take away:

  • whenever transformation happens, use let or run

2. apply and also doesn't transform

2a. pug analogy, part II

Let's do the same thing for this.

Say you have a pug in a trash can. (hint: trash can is not important)

You want it to bark(): "woof!"

After barking, it's still the same old pug.

Here's the code:

val pug: Pug = Pug()
pug.bark()
// after barking, pug is still pug, nothing changes
class Pug {
    fun bark() {
        // Log.d("pug", "woof!") // print log to Android Studio
        // no return, which means, return Unit in Kotlin
    }
}

Before and after .bark(), pug is still pug, nothing changes.

Let's re-write this using apply

val pug: Pug = Pug()
val stillPug = pug.apply { bark() }

Now, using also

val pug: Pug = Pug()
val stillPug = pug.also { it.bark() }

2b. function definition

Take a look at the Standard.kt for the how apply and also are written:

public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }

Notice that now it doesn't have R type, because T the original object type, is returning T after apply or also.

In our case, T is pug, and it remains the same before and after.

2c. Key take away

When there is no transformation, use apply or also.

3. A little confusing, how about renaming?

Most of the developers who I talked to find it also let apply run naming to be confusing. I am wondering if it would be easier to understand if we have a better naming?

Let's try this, Kotlin allows us to import a method name as another name.

import kotlin.apply as perform
import kotlin.run as transform
import kotlin.also as performIt
import kotlin.let as transformIt

Explanation:

  • If there is no transformation, we use perform() or performIt()
  • If there is transformation, we use transform() or transformIt()

Let's check the example use case.

3a. configuration example - perform()

If we need to create a file, and configure it:

    val file = File()
    file.setReadable(true)
    file.setExecutable(true)
    file.setWritable(true)

In the code above, we configure file by running 3 lines of code. At the end, file doesn't change into something else. So no transformation. We use the perform version.

    File().perform {
        setReadable(true)
        setExecutable(true)
        setWritable(true)
    }

In this case, performIt will work too:

    File().perform {
        it.setReadable(true)
        it.setExecutable(true)
        it.setWritable(true)
    }

But perform is better, since we don't really need it

3b. perform task on an object - performIt()

If we need to perform a task on an object, for example, when a crash happens, we want to send the user.id, user.name, and user.country to Crashlytics.

In this case, there is no transformation going on. I choose the performIt()version.

    user.performIt {
        Crashlytics.sendId(it.id)
        Crashlytics.sendName(it.name)
        Crashlytics.sendCountry(it.country)
    }

The perform() will work too.

    user.perform {
        Crashlytics.sendId(id)
        Crashlytics.sendName(name)
        Crashlytics.sendCountry(country)
    }

It's a matter of preference, whether to choose perform or performIt. I don't think we should waste too much time thinking about which to be chosen.

3c. creating view holder - transform

Let's say we have a method to create ViewHolder.

    fun create(parent: ViewGroup): PugViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_pug, parent, false)
        return PugViewHolder(itemView)
    }

We can see that itemView is transformed into PugViewHolder at the end. So we can use the transformIt version.

    fun create(parent: ViewGroup): PugViewHolder {
        return LayoutInflater.from(parent.context).inflate(R.layout.item_pug, parent, false).transformIt {
            PugViewHolder(it)
        }
    }

Again, the transform() version will work too. So I'm not writing 3d.

4. All working together

Consider a case where we need to

  1. create a file
  2. set the file to readable, writable, executable
  3. return the root path of the file
    fun createFile_setMode_returnRootPath(): String {
        val file = File()
        file.setReadable(true)
        file.setExecutable(true)
        file.setWritable(true)
        val rootPath = findRootPath(file)
        return rootPath
    }

re-write using magic functions:

    fun createFile_setMode_returnRootPath(): String {
        return File()
            .perform {
                setReadable(true)
                setExecutable(true)
                setWritable(true)
            }
            .transformIt { findRootPath(it) }
    }

Hope it helps!

Bonus Unicorn Pug.

All pugs are taken from freepik, no pugs are hurt in the making.

Source: https://dev.to/worker8/how-to-use-kotlins-...