Friday, December 1, 2023

Reactive Programming - Convert List[Mono[T]] to Mono[List[T]]

Hello world,

It's been a while, but I'm finally get back into a new groove with programming. My hope for the new year (2024) is to write more on my lessons transitioning to reactive programming.

First, what is different between Reactive Programming vs Functional Programming?

I think this author's article does a splendid job in explaining the difference.

Also, what is different between Reactive Programming vs Imperative Programming?

This article is decent but I think JavaScript is the true language to consider the need for reactive programming due to the ability to process data without order (non-sequentially).

Okay, for now, let's dive into a challenge I had this morning (and I'll need to back track throughout next year). My issue this morning dealt with my need to:

Convert a List<Mono<T>> to a Mono<List<T>

Because, my response to the client needs to be in a Mono<T>; not List<T>.

Since I couldn't figure this out on my on with Mono.map(), Mono.flatMap() and Flux.fromIterable(), I used ChatGPT. You can see my exact question and ChatGPT's solution at the bottom of my conversation here.


Solution (from ChatGPT)

---

To convert a `List<Mono<T>>` to a `Mono<List<T>>` in a reactive programming context using Project Reactor in Java, you can use the `Flux.concat` operator along with `collectList`. Here's an example:


import reactor.core.publisher.Flux;

import reactor.core.publisher.Mono;


import java.util.List;


public class MonoListConversion {


    public static void main(String[] args) {

        // Your original List<Mono<T>>

        List<Mono<String>> monoList = getOriginalMonoList();


        // Convert List<Mono<T>> into Mono<List<T>>

        Mono<List<String>> resultListMono = Flux.concat(monoList).collectList();


        // Now, you have Mono<List<T>> as the result

        resultListMono.subscribe(System.out::println);

    }


    private static List<Mono<String>> getOriginalMonoList() {

        // Your logic to obtain List<Mono<T>>

        // For demonstration purposes, let's create a simple example

        Mono<String> mono1 = Mono.just("Value 1");

        Mono<String> mono2 = Mono.just("Value 2");

        return List.of(mono1, mono2);

    }

}


In this example, `Flux.concat` is used to concatenate the values emitted by the `Mono` instances into a single `Flux`. Then, `collectList` is used to collect the values emitted by the `Flux` into a `Mono<List>>`.

This approach ensures that the values emitted by the `Mono` instances are processed sequentially, maintaining the order of the original list. If the order is not important, you can also use `Flux.merge` instead of `Flux.concat` for concurrent processing.

---


Wednesday, August 9, 2023

Using AI for hacks, attacks

Thanks to IBM researchers, we now have valid proof of the wrongful uses of AI in action. Another reason for AI companies to implement safeguards for societal health.

https://www.axios.com/2023/08/08/ibm-researchers-trick-chatgpt-hacking

Tuesday, August 8, 2023

Nuclear Engines by 2030?

DARPA and NASA aim to test Nuclear Rocket by 2026

"The engine would drastically shorten trips to the moon and Mars."
https://spectrum.ieee.org/nuclear-thermal-rocket

This project, if successful, will launch our way of living into a whole 'notha world. Combined with flying vehicles like EVTOLs, mobility will officially allow people to go have breakfast in the United States, work in Japan, have lunch in Italy, take a training in India, have dinner in Colombia, and then enjoy a restful night in Botswana. All in one day. In addition, people can literally work anywhere whether online or in-person.

Friday, June 23, 2023

Good Tips on GitHub Copilot

https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/

That's it folks! Enjoy!

Oh, also check this out:
https://github.blog/2023-07-17-prompt-engineering-guide-generative-ai-llms/

Thursday, May 18, 2023

Monday, February 6, 2023

ChatGPT, OpenAI, Anthropic?

I wrote about Notion AI a couple months ago. Since then, a lot has happened in the world of AI. OpenAI took headlines with ChatGPT, Microsoft laid off thousands to invest millions into OpenAI, and Google followed suit with also laying off thousands to invest millions into Anthropic AI and announces Bard. Why hasn't such corporations used those millions to invest in their employees - is a question that simply repeats history. For my fellow techmates, beware of the corporate checkmates and steer your own career.

P.S. And then there's Poe, from Quora.

https://openai.com/
https://openai.com/blog/chatgpt/
https://www.anthropic.com/
https://blog.google/technology/ai/bard-google-ai-search-updates/
https://techcrunch.com/2023/02/06/quora-opens-its-new-ai-chatbot-app-poe-to-the-general-public/

Thursday, January 26, 2023