Skip to content

Commit 166c8d8

Browse files
committed
Initial commit
0 parents  commit 166c8d8

15 files changed

+1248
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Uncomment these types if you want even more clean repository. But be careful.
2+
# It can make harm to an existing project source. Read explanations below.
3+
#
4+
# Resource files are binaries containing manifest, project icon and version info.
5+
# They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files.
6+
#*.res
7+
#
8+
# Type library file (binary). In old Delphi versions it should be stored.
9+
# Since Delphi 2009 it is produced from .ridl file and can safely be ignored.
10+
#*.tlb
11+
#
12+
# Diagram Portfolio file. Used by the diagram editor up to Delphi 7.
13+
# Uncomment this if you are not using diagrams or use newer Delphi version.
14+
#*.ddp
15+
#
16+
# Visual LiveBindings file. Added in Delphi XE2.
17+
# Uncomment this if you are not using LiveBindings Designer.
18+
#*.vlb
19+
#
20+
# Deployment Manager configuration file for your project. Added in Delphi XE2.
21+
# Uncomment this if it is not mobile development and you do not use remote debug feature.
22+
#*.deployproj
23+
#
24+
# C++ object files produced when C/C++ Output file generation is configured.
25+
# Uncomment this if you are not using external objects (zlib library for example).
26+
#*.obj
27+
#
28+
29+
# Delphi compiler-generated binaries (safe to delete)
30+
*.exe
31+
*.dll
32+
*.bpl
33+
*.bpi
34+
*.dcp
35+
*.so
36+
*.apk
37+
*.drc
38+
*.map
39+
*.dres
40+
*.rsm
41+
*.tds
42+
*.dcu
43+
*.lib
44+
*.a
45+
*.o
46+
*.ocx
47+
48+
# Delphi autogenerated files (duplicated info)
49+
*.cfg
50+
*.hpp
51+
*Resource.rc
52+
53+
# Delphi local files (user-specific info)
54+
*.local
55+
*.identcache
56+
*.projdata
57+
*.tvsconfig
58+
*.dsk
59+
60+
# Delphi history and backups
61+
__history/
62+
__recovery/
63+
*.~*
64+
65+
# Castalia statistics file (since XE7 Castalia is distributed with Delphi)
66+
*.stat
67+
68+
# Boss dependency manager vendor folder https://github.com/HashLoad/boss
69+
modules/

AnonymousMethos.dpr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
program AnonymousMethos;
2+
3+
{$APPTYPE CONSOLE}
4+
5+
{$R *.res}
6+
7+
uses
8+
Utils in 'src\Utils.pas',
9+
Example.Map in 'src\Example.Map.pas',
10+
Example.Reduce in 'src\Example.Reduce.pas',
11+
Example.Filter in 'src\Example.Filter.pas',
12+
Draft in 'src\Draft.pas';
13+
14+
begin
15+
16+
Reduce;
17+
18+
end.

AnonymousMethos.dproj

+927
Large diffs are not rendered by default.

AnonymousMethos.res

96 Bytes
Binary file not shown.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Samuel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# delphi-anonymus-methods
2+
Using "filter", "map" and "reduce" in Delphi

js/filter.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const frutas = ['uva', 'kiwi', 'banana', 'abacate', 'limão', 'laranja'];
2+
const frutas_com_b = frutas.filter(fruta => fruta.includes('b'));
3+
console.log(frutas_com_b);

js/map.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const animais = ['🐵', '🐮', '🐻', '🦁', '🐺'];
2+
const copia_animais = animais.map(animal => 'Animal: ' + animal);
3+
console.log('Cópia de animais: ', copia_animais)

js/reduce.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const notas = [8.5, 6.5, 7.8, 10];
2+
const somas = notas.reduce((nota, soma) => soma + nota, 0);
3+
console.log('A soma das notas é: ', somas);

src/Draft.pas

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
unit Draft;
2+
3+
interface
4+
5+
type
6+
TAnonimo = reference to procedure;
7+
8+
implementation
9+
10+
uses
11+
System.SysUtils;
12+
13+
var
14+
MetodoAnonimo: TAnonimo;
15+
16+
initialization
17+
MetodoAnonimo := procedure
18+
begin
19+
Writeln('Olá, eu sou um procedimento anônimo!')
20+
end;
21+
22+
end.

src/Example.Filter.pas

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
unit Example.Filter;
2+
3+
interface
4+
5+
procedure Filter;
6+
procedure FilterNumbers;
7+
8+
implementation
9+
10+
uses
11+
System.SysUtils,
12+
Utils;
13+
14+
procedure Filter;
15+
var
16+
Fruta: String;
17+
Frutas: TArray<String>;
18+
FrutasComB: TArray<String>;
19+
begin
20+
Frutas := TArray<String>.Create('Uva', 'Kiwi', 'Banana', 'Abacate', 'Limão', 'Laranja');
21+
FrutasComB := TUtil.Filter<String>(Frutas, function (Fruta: String): Boolean
22+
begin
23+
Result := Fruta.Contains('b') or Fruta.Contains('B');
24+
end);
25+
26+
WriteLn('Frutas que contém a letra "b"');
27+
for Fruta in FrutasComB do
28+
Write(Fruta + ', ');
29+
ReadLn;
30+
end;
31+
32+
procedure FilterNumbers;
33+
var
34+
Numero: Integer;
35+
Numeros: TArray<Integer>;
36+
Filtro: TArray<Integer>;
37+
begin
38+
//Números múltiplos de 3
39+
Numeros := TArray<Integer>.Create(1, 3, 4, 5, 6, 14, 15, 17, 22, 23, 25, 32);
40+
// Filtro := TUtil.Filter<Integer>(Numeros, function (Numero: Integer): Boolean
41+
// begin
42+
// Result := (Numero mod 3 = 0);
43+
// end);
44+
45+
for Numero in Filtro do
46+
Write(Numero.ToString + ', ');
47+
48+
ReadLn;
49+
50+
end;
51+
52+
end.

src/Example.Map.pas

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
unit Example.Map;
2+
3+
interface
4+
5+
procedure Map;
6+
7+
implementation
8+
9+
uses
10+
System.SysUtils,
11+
Utils;
12+
13+
procedure Map;
14+
var
15+
I: String;
16+
Letras: TArray<String>;
17+
LetrasMaiusculas: TArray<String>;
18+
begin
19+
20+
Letras := TArray<String>.Create('a', 'b', 'c', 'd', 'e');
21+
LetrasMaiusculas := TUtil.Map<String>(
22+
Letras, function (Letra: String): String
23+
begin
24+
Result := UpperCase(Letra);
25+
end
26+
);
27+
28+
WriteLn('Letras minúsculas: ');
29+
for I in Letras do
30+
Write(I + ', ');
31+
32+
WriteLn;
33+
WriteLn('Letras maiúsculas: ');
34+
for I in LetrasMaiusculas do
35+
Write(I + ', ');
36+
37+
ReadLn;
38+
39+
end;
40+
41+
end.

src/Example.Reduce.pas

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
unit Example.Reduce;
2+
3+
interface
4+
5+
procedure Reduce;
6+
7+
implementation
8+
9+
uses
10+
System.SysUtils,
11+
Utils;
12+
13+
procedure Reduce;
14+
var
15+
Numeros: TArray<Double>;
16+
Soma: Double;
17+
begin
18+
19+
Numeros := TArray<Double>.Create(8.5, 6.5, 7.8, 10);
20+
Soma := TUtil.Reduce<Double>(Numeros, function (X, Y: Double): Double
21+
begin
22+
Result := X + Y;
23+
end, 0);
24+
25+
WriteLn(Format('Somando os números 8.5, 6.5, 7.8, 10: %.2f', [Soma]));
26+
ReadLn;
27+
end;
28+
29+
end.

src/Utils.pas

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
unit Utils;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils;
7+
8+
type
9+
TUtil = class sealed
10+
class function Map<T>(Args: TArray<T>; F: TFunc<T, T>): TArray<T>;
11+
class function Reduce<T>(Args: TArray<T>; F: TFunc<T, T, T>; InitialValue: T): T;
12+
class function Filter<T>(Args: TArray<T>; F: TFunc<T, Boolean>): TArray<T>;
13+
end;
14+
15+
implementation
16+
17+
uses
18+
System.Generics.Collections;
19+
20+
{ TUtil }
21+
22+
class function TUtil.Filter<T>(Args: TArray<T>; F: TFunc<T, Boolean>): TArray<T>;
23+
var
24+
I: Integer;
25+
List: TList<T>;
26+
begin
27+
List := TList<T>.Create;
28+
try
29+
for I := 0 to Length(Args) - 1 do
30+
if F(Args[I]) then
31+
List.Add(Args[I]);
32+
Result := List.ToArray;
33+
finally
34+
List.Free;
35+
end;
36+
end;
37+
38+
class function TUtil.Map<T>(Args: TArray<T>; F: TFunc<T, T>): TArray<T>;
39+
var
40+
I: Integer;
41+
begin
42+
SetLength(Result, length(Args));
43+
for I := 0 to length(Args) - 1 do
44+
Result[I] := F(Args[I]);
45+
end;
46+
47+
class function TUtil.Reduce<T>(Args: TArray<T>; F: TFunc<T, T, T>; InitialValue: T): T;
48+
var
49+
I: T;
50+
begin
51+
Result := InitialValue;
52+
for I in Args do
53+
Result := F(Result, I);
54+
end;
55+
56+
end.

0 commit comments

Comments
 (0)