Is This a Monad?

References Monad English Wiki: https://en.wikipedia.org/wiki/Monad_(functional_programming) Function English Wiki: https://en.wikipedia.org/wiki/Function_(mathematics) Function Korean Wiki: https://ko.wikipedia.org/wiki/함수 Introduction This time, I want to take a different approach. Instead of explaining what a monad is right away, let’s consider the problems encountered when composing functions, and then present solutions to understand what can be called a monad. So, let’s first revisit what a function is, to improve understanding, and then explain the problematic example. Please note that this post contains personal opinions. As the title suggests, I can’t confidently say “this is a monad!” ...

June 25, 2023

Stream API - peek

peek? Why does the documentation recommend using Stream.peek mainly for debugging? Let’s find out. Reference Stream API Since peek is part of the Stream API, let’s start by looking at the Stream API documentation. According to the Stream API documentation, functions passed to methods like filter and map must satisfy two constraints for correct operation: They must be non-interfering In most cases, they should be stateless Let’s focus on non-interfering. non-interfering Dictionary meaning of interfering Interfering, meddlesome ...

December 13, 2022

Lambda Expression and Method Reference Differences

Users Sort by last name If the last names are the same, sort by first name Then return as a List. I created an example for cases where there are two sorting criteria. And I tried to solve the problem using method reference and lambda expression… but what happened… The example written with a lambda expression results in a compile error. Huh! Is it not able to infer the type well…? They look exactly the same, though. ...

October 13, 2022

Stream API Practice

Recently, I read the book Modern Java In Action. I was very impressed. I realized that when studying functional programming and the Java API, I should read books. Compared to Googling, books are much richer in content and much more reliable. After all, it was written by a Java Champion, two professors, and one engineer! Anyway, I will leave some code I wrote while studying the stream API. And I will gradually share the ideas behind why I wrote the code this way. ...

October 13, 2022

Asynchronous Programming - Summary

I will elaborate further through Java API and technical blog introductions in the future. Asynchronous What is Asynchronous? Processing tasks not in sequential order. When is it used? Used for performance improvement. When there are I/O operations When there are CPU bounded jobs (batch jobs) Considerations when writing asynchronous programs How to use the results of asynchronous tasks? callback Pass a function (routine) to perform additional work upon completion. blocking Wait until all are completed using buffer Able to use data even if I/O is not finished What state are the asynchronous tasks in? Responding according to state If all are completed -> process If in progress -> wait or proceed to the next process If an error occurred -> fail the entire process, etc. How to handle asynchronous I/O tasks? non-blocking (event loop method) blocking (request I/O to Thread in the usual way) When splitting multiple jobs, how to handle completion or failure If all are completed, success How to determine if all succeeded? If even one fails, all fail (fail fast) How to determine if one failed? If even one is completed, treat as success How to determine? Process in the order of completion Concurrency Since the purpose of asynchronous tasks is performance improvement, multiple threads may be used. In this case, concurrency issues must be resolved when accessing the same resource. volatile Ensures that the value referenced is from main memory, not cached in the CPU register (to resolve shared resource inconsistency issues) CompareAndSwap Ensures atomicity of operations at the CPU level for Check Then Act. Only one thread can perform the operation. Changes the value of a variable only if it contains the expected value. Used instead of the synchronization keyword (lock). If a thread is blocked, it cannot immediately wake up from the blocked state. ForkJoinPool Work-stealing technique. Threads can steal jobs assigned to other threads. Tasks cannot be perfectly evenly distributed among threads. Therefore, idle threads can take tasks from busy threads. Asynchronous processing at the system level? Use a message queue The producer sends a message about the work to the queue, and the consumer reads the message and processes the work Message queues are also used to decouple dependencies in event-driven design Transaction unit Group up to message publishing as a single transaction. Whether the asynchronous task is completed is not of concern. You can think of the message as a task. If the consumer fails to process due to an error, re-publishing is necessary.

August 29, 2022

Two's Complement

Two’s Complement Introduction In this post, I want to talk about two’s complement. You might wonder, “Is it necessary to write about such a simple topic?” But even after reading many articles, there were parts I personally didn’t fully understand. Here are the questions I had: What is two’s complement? What is one’s complement? What is a complement? Of course, the methods for finding one’s and two’s complement are widely known. For example: ...

July 15, 2022

Simplified Transaction

Example Code https://github.com/jurogrammer/dtrans Situation When you want to separate transactions for different operations, but creating a new service class for this feels cumbersome. Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Service @RequiredArgsConstructor public class SaveServiceVer1 { private final UserRepository userRepository; private final OrderRepository orderRepository; @Transactional public void save() { userRepository.save(new User("홍길동")); // If an error occurs while saving the order, you still want the user to be saved as is. saveOrder(); } public void saveOrder() { orderRepository.save(new Order()); throw new RuntimeException("Error occurred while saving order"); } } Idea Even if an error occurs in saveOrder and the order is rolled back, the User save should still be performed. ...

July 15, 2022

Collections Summary - Interfaces

Related Posts https://jurogrammer.tistory.com/172 Interfaces https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html Overview Core collection interfaces encapsulate different types of collections. Therefore, you can manipulate different collections without worrying about the details. The collection interface can be considered the foundation of the Java Collection Framework. The Interface chapter will provide general guidelines for efficient use of collection interfaces. Important Notes Map is not a Collection Literally, it’s not. All Core collections are Generic When declaring a collection, you must specify the type that will go into the collection. Generics can verify at compile time whether the object being put into the collection is of the correct type. Java Platform does not provide interface variations To facilitate the management of numerous core collection interfaces, the Java Platform does not provide interface variations (e.g., interfaces with characteristics like immutable, fixed-size, append-only). Instead, modification operations are optional. That is, since not all operations are implemented, calling a specific operation may throw an UnsupportedOperationException. Therefore, this must be documented. In summary, if you need to implement a specific collection interface, implement only what you need. Don’t implement everything. ...

June 30, 2022

TLS Packet Analysis

TLS Packet Analysis Reference Wikipedia: Transport Layer Security RFC 5246 Previously, we looked at the TCP 3-way handshake. In this post, we will examine TLS communication. Since many blogs have already covered the basics of TLS, I will write this for personal study, comparing it with the RFC documentation. In the next post, I will cover decrypting the encrypted application layer data. Remember, TLS is also a stateful, connection-oriented protocol. ...

June 24, 2022

TCP, IP Packet Analysis with Wireshark

Packet Analysis Packet Analysis Goals View IP packet header View TCP packet header See 3-way handshake Reference Page https://support.microsoft.com/en-us/topic/how-to-use-tracert-to-troubleshoot-tcp-ip-problems-in-windows-e643d72b-2f4f-cdd6-09a0-fd2989c7ca8e Filtering for Clarity First, find the part where the 3-way handshake is done with TCP… (I found the [SYN] part by eye) Filter by IP and port IP: Filter packets by sender and receiver to the target host. TCP: Set the sending and receiving ports to the client port. You can see only the communication between a specific client and server on localhost. ...

June 22, 2022