Skip to content

Instantly share code, notes, and snippets.

@ku-kim

ku-kim/App.java Secret

Last active September 3, 2022 12:10
Show Gist options
  • Save ku-kim/12f52c59b8133c6ce58b97321dfac632 to your computer and use it in GitHub Desktop.
Save ku-kim/12f52c59b8133c6ce58b97321dfac632 to your computer and use it in GitHub Desktop.
CS10-06 : 정수 분류기 (함수형 프로그래밍)

CS10-06 : 정수 분류기 (함수형 프로그래밍)

이 프로젝트는 함수형 프로그래밍을 연습합니다.


1단계 : 순수함수 만들기

  • 기존 코드를 함수형으로 변경하고 코드양을 줄인다.

변경 사항

    public Set factors() {
        HashSet factors = new HashSet<>();
        for (int pod=1; pod <= Math.sqrt(number); pod++) {
            if (isFactor(pod)) {
                factors.add(pod);
                factors.add(number / pod);
            }
        }
        return factors;
    }
    
    
// -> 스트림 & 람다 변경    
    public Set factors() {
            Set<Integer> factors = IntStream.rangeClosed(1, (int) Math.sqrt(number))
            .filter((i) -> number % i == 0)
            .mapToObj((i) -> List.of(i, number / i))
            .collect(HashSet::new, HashSet::addAll, HashSet::addAll);
    
            return factors;
            }
    static public int sum(Set factors) {
        Iterator iterator = factors.iterator();
        int sum = 0;
        while (iterator.hasNext()) {
            sum += (Integer) iterator.next();
        }
        return sum;
    }
    
// -> 스트림 & 람다 변경    
    static public int sum(Set factors) {
            return factors.stream().mapToInt(i-> (int) i).sum();
            }

2단계 : 고차함수 활용

  • map, filter, reduce를 활용한다. (구현 X)

Hits

import java.util.stream.IntStream;
public class App {
public static void main(String[] args) {
IntStream.rangeClosed(2, 100)
.filter()
.reduce(ClassifierAlpha::new)
.forEach(System.out::println);
}
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class ClassifierAlpha {
private int number;
public ClassifierAlpha(int number) {
this.number = number;
}
public Set factors() {
Set<Integer> factors = IntStream.rangeClosed(1, (int) Math.sqrt(number))
.filter((i) -> number % i == 0)
.mapToObj((i) -> List.of(i, number / i))
.collect(HashSet::new, HashSet::addAll, HashSet::addAll);
return factors;
}
static public int sum(Set factors) {
return factors.stream().mapToInt(i-> (int) i).sum();
}
public boolean isPerfect() {
return sum(factors()) - number == number;
}
public boolean isAbundant() {
return sum(factors()) - number > number;
}
public boolean isDeficient() {
return sum(factors()) - number < number;
}
public static void main(String[] args) {
ClassifierAlpha alpha1 = new ClassifierAlpha(10);
ClassifierAlpha alpha2 = new ClassifierAlpha(6);
System.out.println(alpha1.isPerfect());
System.out.println(alpha2.isPerfect());
}
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class ClassifierAlphaPrev {
private int number;
public ClassifierAlphaPrev(int number) {
this.number = number;
}
public boolean isFactor(int potentialFactor) {
return number % potentialFactor == 0;
}
public Set factors() {
HashSet factors = new HashSet<>();
for (int pod=1; pod <= Math.sqrt(number); pod++) {
if (isFactor(pod)) {
factors.add(pod);
factors.add(number / pod);
}
}
return factors;
}
static public int sum(Set factors) {
Iterator iterator = factors.iterator();
int sum = 0;
while (iterator.hasNext()) {
sum += (Integer) iterator.next();
}
return sum;
}
public boolean isPerfect() {
return sum(factors()) - number == number;
}
public boolean isAbundant() {
return sum(factors()) - number > number;
}
public boolean isDeficient() {
return sum(factors()) - number < number;
}
public static void main(String[] args) {
ClassifierAlphaPrev alpha1 = new ClassifierAlphaPrev(10);
ClassifierAlphaPrev alpha2 = new ClassifierAlphaPrev(6);
System.out.println(alpha1.isPerfect());
System.out.println(alpha2.isPerfect());
}
}
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
public class ClassifierAlphaTest {
@ParameterizedTest(name = "Test num : {0}")
@MethodSource("argsProviderFactory")
void all_1_100(int i) {
ClassifierAlphaPrev expected = new ClassifierAlphaPrev(i);
ClassifierAlpha sut = new ClassifierAlpha(i);
assertThat(sut.isPerfect()).isEqualTo(expected.isPerfect());
}
static IntStream argsProviderFactory() {
return IntStream.rangeClosed(1, 100);
}}
import java.util.HashSet;
import java.util.Set;
public class PrimeAlpha {
private int number;
public PrimeAlpha(int number) {
this.number = number;
}
public boolean isPrime() {
Set primeSet = new HashSet(){ {add(1); add(number);} };
return number > 1 && factors().equals(primeSet);
}
public boolean isFactor(int potentialFactor) {
return number % potentialFactor == 0;
}
public Set factors() {
HashSet factors = new HashSet<>();
for (int pod=1; pod <= Math.sqrt(number); pod++) {
if (isFactor(pod)) {
factors.add(pod);
factors.add(number / pod);
}
}
return factors;
}
public static void main(String[] args) {
PrimeAlpha prime1 = new PrimeAlpha(10);
PrimeAlpha prime2 = new PrimeAlpha(7);
System.out.println(prime1.isPrime());
System.out.println(prime2.isPrime());
}
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
public class PrimeAlphaPrev {
private int number;
public PrimeAlphaPrev(int number) {
this.number = number;
}
public boolean isPrime() {
Set primeSet = new HashSet(){ {add(1); add(number);} };
return number > 1 && factors().equals(primeSet);
}
public Set factors() {
Set<Integer> factors = IntStream.rangeClosed(1, (int) Math.sqrt(number))
.filter((i) -> number % i == 0)
.mapToObj((i) -> List.of(i, number / i))
.collect(HashSet::new, HashSet::addAll, HashSet::addAll);
return factors;
}
public static void main(String[] args) {
PrimeAlphaPrev prime1 = new PrimeAlphaPrev(10);
PrimeAlphaPrev prime2 = new PrimeAlphaPrev(7);
System.out.println(prime1.isPrime());
System.out.println(prime2.isPrime());
}
}
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
public class PrimeAlphaTest {
@ParameterizedTest(name = "Test num : {0}")
@MethodSource("argsProviderFactory")
void all_1_100(int i) {
PrimeAlphaPrev expected = new PrimeAlphaPrev(i);
PrimeAlpha sut = new PrimeAlpha(i);
assertThat(sut.isPrime()).isEqualTo(expected.isPrime());
}
static IntStream argsProviderFactory() {
return IntStream.rangeClosed(1, 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment